使用Topshelf建立Windows 服务

Winndows Service 是一种可随 Windows 操做系统启动而启动的,在后台运行的,一般不和用户产生交互的程序。它没法经过双击来运行,相似于 Unix 守护进程(daemon processes),当用户注销时它也不会中止。app

Windows 服务由三部分组成:框架

  • 一个服务可执行文件;
  • 一个服务控制程序(SCP);
  • 服务控制管理器(SCM),负责在 HKLM"SYSTEM"CurrentControlSet"Services 下建立服务键值。用户可经过 SCP 控制服务的启动、中止、暂停等,SCP 会经过 SCM 调用服务程序

开发一个Windows服务一般也比较简单,在开发的时候咱们指望以命令行方式运行,想对Windows服务有更多的控制,就有一个Windows服务框架TopShelf 能够知足,使用这个框架要求你使用一个IoC容器,在框架中使用的是common service locator 接口,能够根据你的喜爱去选择你本身中意的IoC容器。google

TopShelf的基本介绍能够参看Dru Sellers 的介绍性文章 TopShelf。下面的代码就是建立了一个Windows服务:spa

using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Timers; 
    using log4net.Config; 
    using Microsoft.Practices.ServiceLocation; 
    using StructureMap; 
    using Topshelf; 
    using Topshelf.Configuration;操作系统

    internal class Program 
    { 
        static void Main(string[] args) 
        { 
            XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config")); 
            IRunConfiguration cfg = RunnerConfigurator.New(x => 
            { 
                x.AfterStoppingTheHost(h => { Console.WriteLine("AfterStop called invoked, services are stopping"); });命令行

                 x.SetDescription("Sample Topshelf Host"); 
                x.SetDisplayName("Stuff"); 
                x.SetServiceName("stuff"); 调试

                x.ConfigureServiceInIsolation<TownCrier>("tc", s => 
                { 
                    s.CreateServiceLocator(()=> 
                    { 
                        ObjectFactory.Initialize(i => 
                        { 
                            i.ForConcreteType<TownCrier>().Configure.WithName("tc"); 
                        });code

                        return new StructureMapServiceLocator(); 
                    }); 
                    s.WhenStarted(tc => tc.Start()); 
                    s.WhenStopped(tc => tc.Stop()); 
                }); xml

                x.RunAsLocalSystem();blog

               });

            Runner.Host(cfg, args); 
        } 
    }

这里咱们使用了StructureMap 做为IoC容器,建立了一个StructureMapServiceLocator来掩藏StructureMap,建立的Windows服务的名称是stuff,能够吊相应的方法启动,中止服务。经过直接运行.exe文件在控制台中运行或者调试服务了。

经过命令运行,安装卸载Windows服务

Stuff.exe                #控制台方式运行

Stuff.exe /install     #安装Windows服务

Stuff.exe /uninstall  #卸载Windows服务

默认状况下,Windows服务只能运行一个实例,若是咱们想运行多个实例怎么办,能够在Topshelf的命令行参数中增长–instance <instance name>来指定实例的名称,也能够经过运行时读取配置文件来达到目的,我更喜欢使用后一种方式设置,在应用程序的配置文件上增长个配置WindowsServiceInstanceName:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

   <appSettings file="applicationSettings.config">

    <add key="WindowsServiceInstanceName" value="Stuff"/>

   </appSettings>

</configuration>

而后改造一下上述代码

       static void Main(string[] args) 
        {

            var instanceName = ConfigurationManager.AppSettings["WindowsServiceInstanceName"];

            XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config")); 
            IRunConfiguration cfg = RunnerConfigurator.New(x => 
            { 
                x.AfterStoppingTheHost(h => { Console.WriteLine("AfterStop called invoked, services are stopping"); });

                x.SetDescription("Sample Topshelf Host"); 
                x.SetDisplayName(instanceName ); 
                x.SetServiceName(instanceName ); 
……

这样咱们就能够达到在同台机器上安装多个Windows 服务实例,推荐你们使用这个Windows服务框架TopShelf ,能够简化不少工做和增长灵活性

相关文章
相关标签/搜索