WindowsService调用API 一、创建一个Windows Service 二、创建服务安装程序 三、写入服务代码 四、创建安装脚本 五、在C#中对服务进行控制 六、需要在ServiceTest的APP.Config下配置如下信息 七、总结

本文着重于WindowsServic如何调用API以及出现部分问题的解决方案

本文Windows Service 创建摘自JasperXu的博客   链接:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html

C#创建Windows Service(Windows 服务)基础教程

 

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。

本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。

1)创建Windows Service项目

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

2)对Service重命名

将Service1重命名为你服务名称,这里我们命名为ServiceTest。

二、创建服务安装程序

1)添加安装程序

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。

2)修改安装服务名

右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

3)修改安装权限

右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

三、写入服务代码

1)打开ServiceTest代码

右键ServiceTest,选择查看代码。

2)写入Service逻辑

添加如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsServiceTest {  public partial class ServiceTest : ServiceBase  {        

//计时器必须要是全局      

  System.Timers.Timer timer1;   public ServiceTest()   {    InitializeComponent();   }

        /// <summary>

        ///服务开始  

       ///此处需要根据实际情况来决定,如果想让服务调用接口(使用System.Timers.Timer计时器)       

     /// </summary>        

   /// <param name="args"></param>

  protected override void OnStart(string[] args)   {        

          timer1 = new System.Timers.Timer();

          timer1.Interval = 1200000;  //设置计时器事件间隔执行时间20分钟执行一次          

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(StartIntegrate);

            timer1.Enabled = true;   

  }        

/// <summary>        

/// 服务结束        

/// </summary>   

protected override void OnStop()   {                

            }

        /// <summary>  

       /// 这里是要写入调用接口的方法了

        /// </summary>        

/// <param name="sender"></param>        

/// <param name="e"></param>        

public static void StartIntegrateTest(object sender, System.Timers.ElapsedEventArgs e) {

            int  hour = DateTime.Now.Hour;//当前时间  小时

            int  minu = DateTime.Now.Minute;//当前时间 分                      

//判断时间是否为0点整  

           if (hour == 2 && minu <= 59)   

          {                

        string ContractorCode = "10010021";

                string year = DateTime.Now.Year.ToString();                          

      string edate = DateTime.Now.ToString("yyyy-MM-dd");//结束时间为当前时间

                string sdate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");//开始当前时间-24H

                //以下方法为接口的方法

                DAL.Integrate.IntegrateContractorInfo(ContractorCode); 

                DAL.Integrate.IntegrateContractorTrainInfoByTime(ContractorCode, sdate, edate);//

                DAL.Integrate.IntegrateContractorQualifiedInfo(ContractorCode, year);  

                DAL.Integrate.IntegrateEnterpriseTrainRecord(ContractorCode, year);

                DAL.Integrate.IntegrateEnterpriseTrainRecordUser(ContractorCode, sdate, edate);

                DAL.Integrate.IntegrateEnterpriseTrainCertificate(ContractorCode, sdate, edate);                       }

        }

    } }

四、创建安装脚本

在项目中添加2个文件如下(必须是ANSI或者UTF-8无BOM格式):

1)安装脚本Install.bat

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe WindowsServiceTest.exe
Net Start ServiceTest
sc config ServiceTest start= auto
::pause
2)卸载脚本Uninstall.bat
%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u WindowsServiceTest.exe
::pause
3)安装脚本说明

第二行为启动服务。

第三行为设置服务为自动运行。

这2行视服务形式自行选择。

4)脚本调试

如果需要查看脚本运行状况,在脚本最后一行加入pause

五、在C#中对服务进行控制

0)配置目录结构

简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。

在WindowsServiceTestUI的binDebug目录下建立Service目录。

将WindowsServiceTest的生成目录设置为上面创建的Service目录。

生成后目录结构如下图

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

1)安装

安装时会产生目录问题,所以安装代码如下:

        /// <summary>
        /// 安装
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnInstall_Click(object sender, RoutedEventArgs e)
  {
   string CurrentDirectory = System.Environment.CurrentDirectory;
   System.Environment.CurrentDirectory = CurrentDirectory + "\Service";
   Process process = new Process();
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.FileName = "Install.bat";
   process.StartInfo.CreateNoWindow = true;
   process.Start();
   lblLog.Text = "安装成功";
   System.Environment.CurrentDirectory = CurrentDirectory;
  }

2)卸载

卸载时也会产生目录问题,所以卸载代码如下:

     /// <summary>
        /// 卸载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnUninstall_Click(object sender, RoutedEventArgs e)
  {
   string CurrentDirectory = System.Environment.CurrentDirectory;
   System.Environment.CurrentDirectory = CurrentDirectory + "\Service";
   Process process = new Process();
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.FileName = "Uninstall.bat";
   process.StartInfo.CreateNoWindow = true;
   process.Start();
   lblLog.Text = "卸载成功";
   System.Environment.CurrentDirectory = CurrentDirectory;
  }
 
3)启动

代码如下:

  /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnStart_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   serviceController.Start();
   lblStatus.Text = "服务已启动";
  }
4)停止
    /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnStop_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   if (serviceController.CanStop)
   {
    serviceController.Stop();
    lblStatus.Text = "服务已停止";
   }
   else
    lblStatus.Text = "服务不能停止";
  }

5)暂停/继续

    /// <summary>
        /// 暂停/继续服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnPauseContinue_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   if (serviceController.CanPauseAndContinue)
   {
    if (serviceController.Status == ServiceControllerStatus.Running)
    {
     serviceController.Pause();
     lblStatus.Text = "服务已暂停";
    }
    else if (serviceController.Status == ServiceControllerStatus.Paused)
    {
     serviceController.Continue();
     lblStatus.Text = "服务已继续";
    }
    else
    {
     lblStatus.Text = "服务未处于暂停和启动状态";
    }
   }
   else
    lblStatus.Text = "服务不能暂停";
  }

6)检查状态

       /// <summary>
        /// 服务状态监测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnCheckStatus_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   lblCheckStatus.Text = serviceController.Status.ToString();
  }

六、需要在ServiceTest的APP.Config下配置如下信息

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="" connectionString="" providerName="System.Data.EntityClient" />//写入地址等相关信息
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

七、总结

在发布编译文件,并运行时,有时候会出现Stopping导致服务一直无法关闭,运行会直接报错,此时需要你在进程中关掉服务之后,以管理员身份运行该运用程序

示例代码请见:https://github.com/sorex/WindowsServiceTest

Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的。所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Windows Service写很深入。

本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。

1)创建Windows Service项目

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

2)对Service重命名

将Service1重命名为你服务名称,这里我们命名为ServiceTest。

二、创建服务安装程序

1)添加安装程序

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。

2)修改安装服务名

右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

3)修改安装权限

右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

三、写入服务代码

1)打开ServiceTest代码

右键ServiceTest,选择查看代码。

2)写入Service逻辑

添加如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsServiceTest {  public partial class ServiceTest : ServiceBase  {        

//计时器必须要是全局      

  System.Timers.Timer timer1;   public ServiceTest()   {    InitializeComponent();   }

        /// <summary>

        ///服务开始  

       ///此处需要根据实际情况来决定,如果想让服务调用接口(使用System.Timers.Timer计时器)       

     /// </summary>        

   /// <param name="args"></param>

  protected override void OnStart(string[] args)   {        

          timer1 = new System.Timers.Timer();

          timer1.Interval = 1200000;  //设置计时器事件间隔执行时间20分钟执行一次          

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(StartIntegrate);

            timer1.Enabled = true;   

  }        

/// <summary>        

/// 服务结束        

/// </summary>   

protected override void OnStop()   {                

            }

        /// <summary>  

       /// 这里是要写入调用接口的方法了

        /// </summary>        

/// <param name="sender"></param>        

/// <param name="e"></param>        

public static void StartIntegrateTest(object sender, System.Timers.ElapsedEventArgs e) {

            int  hour = DateTime.Now.Hour;//当前时间  小时

            int  minu = DateTime.Now.Minute;//当前时间 分                      

//判断时间是否为0点整  

           if (hour == 2 && minu <= 59)   

          {                

        string ContractorCode = "10010021";

                string year = DateTime.Now.Year.ToString();                          

      string edate = DateTime.Now.ToString("yyyy-MM-dd");//结束时间为当前时间

                string sdate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");//开始当前时间-24H

                //以下方法为接口的方法

                DAL.Integrate.IntegrateContractorInfo(ContractorCode); 

                DAL.Integrate.IntegrateContractorTrainInfoByTime(ContractorCode, sdate, edate);//

                DAL.Integrate.IntegrateContractorQualifiedInfo(ContractorCode, year);  

                DAL.Integrate.IntegrateEnterpriseTrainRecord(ContractorCode, year);

                DAL.Integrate.IntegrateEnterpriseTrainRecordUser(ContractorCode, sdate, edate);

                DAL.Integrate.IntegrateEnterpriseTrainCertificate(ContractorCode, sdate, edate);                       }

        }

    } }

四、创建安装脚本

在项目中添加2个文件如下(必须是ANSI或者UTF-8无BOM格式):

1)安装脚本Install.bat

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe WindowsServiceTest.exe
Net Start ServiceTest
sc config ServiceTest start= auto
::pause
2)卸载脚本Uninstall.bat
%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u WindowsServiceTest.exe
::pause
3)安装脚本说明

第二行为启动服务。

第三行为设置服务为自动运行。

这2行视服务形式自行选择。

4)脚本调试

如果需要查看脚本运行状况,在脚本最后一行加入pause

五、在C#中对服务进行控制

0)配置目录结构

简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。

在WindowsServiceTestUI的binDebug目录下建立Service目录。

将WindowsServiceTest的生成目录设置为上面创建的Service目录。

生成后目录结构如下图

WindowsService调用API
一、创建一个Windows Service
二、创建服务安装程序
三、写入服务代码
四、创建安装脚本
五、在C#中对服务进行控制
六、需要在ServiceTest的APP.Config下配置如下信息
七、总结

1)安装

安装时会产生目录问题,所以安装代码如下:

        /// <summary>
        /// 安装
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnInstall_Click(object sender, RoutedEventArgs e)
  {
   string CurrentDirectory = System.Environment.CurrentDirectory;
   System.Environment.CurrentDirectory = CurrentDirectory + "\Service";
   Process process = new Process();
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.FileName = "Install.bat";
   process.StartInfo.CreateNoWindow = true;
   process.Start();
   lblLog.Text = "安装成功";
   System.Environment.CurrentDirectory = CurrentDirectory;
  }

2)卸载

卸载时也会产生目录问题,所以卸载代码如下:

     /// <summary>
        /// 卸载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnUninstall_Click(object sender, RoutedEventArgs e)
  {
   string CurrentDirectory = System.Environment.CurrentDirectory;
   System.Environment.CurrentDirectory = CurrentDirectory + "\Service";
   Process process = new Process();
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.FileName = "Uninstall.bat";
   process.StartInfo.CreateNoWindow = true;
   process.Start();
   lblLog.Text = "卸载成功";
   System.Environment.CurrentDirectory = CurrentDirectory;
  }
 
3)启动

代码如下:

  /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnStart_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   serviceController.Start();
   lblStatus.Text = "服务已启动";
  }
4)停止
    /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnStop_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   if (serviceController.CanStop)
   {
    serviceController.Stop();
    lblStatus.Text = "服务已停止";
   }
   else
    lblStatus.Text = "服务不能停止";
  }

5)暂停/继续

    /// <summary>
        /// 暂停/继续服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnPauseContinue_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   if (serviceController.CanPauseAndContinue)
   {
    if (serviceController.Status == ServiceControllerStatus.Running)
    {
     serviceController.Pause();
     lblStatus.Text = "服务已暂停";
    }
    else if (serviceController.Status == ServiceControllerStatus.Paused)
    {
     serviceController.Continue();
     lblStatus.Text = "服务已继续";
    }
    else
    {
     lblStatus.Text = "服务未处于暂停和启动状态";
    }
   }
   else
    lblStatus.Text = "服务不能暂停";
  }

6)检查状态

       /// <summary>
        /// 服务状态监测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
  private void btnCheckStatus_Click(object sender, RoutedEventArgs e)
  {
   ServiceController serviceController = new ServiceController("ServiceTest");
   lblCheckStatus.Text = serviceController.Status.ToString();
  }

六、需要在ServiceTest的APP.Config下配置如下信息

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="" connectionString="" providerName="System.Data.EntityClient" />//写入地址等相关信息
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

七、总结

在发布编译文件,并运行时,有时候会出现Stopping导致服务一直无法关闭,运行会直接报错,此时需要你在进程中关掉服务之后,以管理员身份运行该运用程序

示例代码请见:https://github.com/sorex/WindowsServiceTest