2010年11月16日星期二

Create Windows Service by C#

.Net 下创建Windows Service 非常简单。

1 继承ServiceBase,根据需要Override OnStart, OnStop, OnPauce, OnContinue,OnShutDown等方法。
所以并不是所有的基类虚方法都要重写的,一般来说写一个OnStart就够了。
例如
public partial class HellowordService : ServiceBase
    {
        public HellowordService()
        {
            // ServiceBase继承自Component,所以也是个组件,可以直接将想要的服务
            // 分线程Component直接托近来使用,比如将FileSystemWatcher拖进来监视系统文件状况
            // 还可以拖个Backgroudworker近来,将具体的逻辑委托出去。
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            Console.WriteLine("Hello word!");
        }
    }
再来一个HelloWxgService
public partial class HelloWxgService: ServiceBase
    {
        public HelloWxgService()
        {
              InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            Console.WriteLine("Hello wxg!");
        }
    }

2 具体的Service类做好后,要做个Service安装类。
安装类要继承Installer基类,也是个组件,而且还要对这个继承类声明成执行安装属性
[RunInstaller(true)],是为了通过.net的安装工具installUtil安装时,告诉installUtil,安装入口是谁。
例:
    [RunInstaller(true)]
    public partial class WSManagerInstaller : Installer
    {
        public WSManagerInstaller ()
        {
            InitializeComponent();
        }
    }

3 Installer的子类HellowordServiceInstaller 中要添加ServiceInstaller和ServiceProcessInstaller。
一个Service,对应一个ServiceInstaller, 而ServiceProcessInstaller 就一个就够了。
ServiceProcessInstaller 是在InstallUtil安装服务时,写注册表用的安装类。
ServiceInstaller.ServiceName 设定值将在控制面板-管理工具-Service的列表中显示。
例:
   private ServiceInstaller serviceInstaller1;
   private ServiceInstaller serviceInstaller2;
   private ServiceProcessInstaller processInstaller;

   private InitializeComponent(){

      // Instantiate installers for process and services.
      processInstaller = new ServiceProcessInstaller();
      serviceInstaller1 = new ServiceInstaller();
      serviceInstaller2 = new ServiceInstaller();

      // The services run under the system account.
      processInstaller.Account = ServiceAccount.LocalSystem;
      processInstaller.User=null;
      processInstaller.Password=null;

      // The services are started manually.
      serviceInstaller1.StartType = ServiceStartMode.Manual;
      serviceInstaller2.StartType = ServiceStartMode.Manual;

      // ServiceName must equal those on ServiceBase derived classes.           
      serviceInstaller1.ServiceName = "TestFileWatherService";
      serviceInstaller2.ServiceName = "HelloewordService";

      // Add installers to collection. Order is not important.
      this.Installers.Add(serviceInstaller1);
      this.Installers.Add(serviceInstaller2);
      this.Installers.Add(processInstaller);
   }
4 Installer类做成后,通过InstallUtil.exe执行安装/卸载
InstallUtil.exe的路径是
%WINDOWS%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe
安装命令:
%WINDOWS%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe  TestServices.exe
卸载命令(增加 -u ):
%WINDOWS%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe  -u TestServices.exe

以上,安装完之后,就可以通过windows的Service管理工具查看,并启动或停止。
注意:以上的例子中将服务设定成手动启动,
安装完后,可能要重起PC才有效,不然总是会出1083的服务不能启动错误。

没有评论: