Topself 方便调试的Window服务框架

Installing Topshelf

nuget Install-Package Topshelf

public class TownCrier
{
    readonly Timer _timer;
    public TownCrier()
    {
        _timer = new Timer(1000) {AutoReset = true};
        _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
    }
    public void Start() { _timer.Start(); }
    public void Stop() { _timer.Stop(); }
}

public class Program
{
    public static void Main()
    {
        HostFactory.Run(x =>                                 //1
        {
            x.Service<TownCrier>(s =>                        //2
            {
               s.ConstructUsing(name=> new TownCrier());     //3
               s.WhenStarted(tc => tc.Start());              //4
               s.WhenStopped(tc => tc.Stop());               //5
            });
            x.RunAsLocalSystem();                            //6

            x.SetDescription("Sample Topshelf Host");        //7
            x.SetDisplayName("Stuff");                       //8
            x.SetServiceName("Stuff");                       //9
        });                                                  //10
    }
}

可以直接运行程序,也可以安装到windows服务后台了。

安装方法:

YourApp.exe install
YourApp.exe uninstall

YourApp.exe start
YourApp.exe stop

看起来是不是比.net自带的installutil.exe 方便一百倍?

原文地址:https://www.cnblogs.com/sgciviolence/p/5784354.html