Kiba518

Kiba518

三线城市架构师一枚。

Fork me on GitHub

使用Topshelf创建Windows服务【转载加再编辑】

概述

Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务。Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。

引用安装

1、官网:http://topshelf-project.com/  这里面有详细的文档及下载

2、Topshelf的代码托管在 http://github.com/topshelf/Topshelf/downloads   ,可以在这里下载到最新的代码。

3、新建一个项目,只需要引用Topshelf.dll 即可,为了日志输出显示,建议也同时引用Topshelf.Log4Net。程序安装命令

  • Install-Package Topshelf
  • Install-Package Topshelf.Log4Net

使用

官网文档给过来的例子非常简单,直接使用即可以跑起来,官网文档地址:http://docs.topshelf-project.com/en/latest/configuration/quickstart.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Topshelf;
//安装服务 F:\Test\QuartzConsole\TopSelf\bin\Debug\TopSelf.exe install
//卸载服务 F:\Test\QuartzConsole\TopSelf\bin\Debug\TopSelf.exe uninstall
//启动服务 F:\Test\QuartzConsole\TopSelf\bin\Debug\TopSelf.exe start
namespace TopSelf
{
    public class Kiba1
    {
        readonly Timer _timer;
        public Kiba1()
        {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => Console.WriteLine($"{DateTime.Now} Kiba1 running");
        }
        public void Start() { _timer.Start(); }
        public void Stop() { _timer.Stop(); }
    }
   
    public class Program
    {
        public static void Main()                                
        {                                                        
            HostFactory.Run(x =>                                 
            {                                                    
                x.Service<Kiba1>(s =>                        
                {                                                
                    s.ConstructUsing(name => new Kiba1()); //使用Kiba1类进行构建服务
                    s.WhenStarted(tc => tc.Start());       //使用Kiba1的Start作为启动函数
                    s.WhenStopped(tc => tc.Stop());        //使用Kiba1的Stop作为停止函数
                });                                              
                x.RunAsLocalSystem();      //服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如:x.RunAs("username", "password");  x.RunAsPrompt(); x.RunAsNetworkService(); 等                      

                x.SetDescription("服务描述");           
                x.SetDisplayName("服务显示名");                  
                x.SetServiceName("服务名");                      
            });                                                  
        }

        
    }
}

配置运行

没错,整个程序已经开发完了,接下来,只需要简单配置一下,即可以当服务来使用了。安装很方便:

安装:TopshelfDemo.exe install
启动:TopshelfDemo.exe start
卸载:TopshelfDemo.exe uninstall

安装成功后,接下来,我们就可以看到服务里多了一个服务:

第二种运行方式

使用继承ServiceControl的类作为服务。代码如下:

   public class Kiba1: ServiceControl
    {
        readonly Timer _timer;
        public Kiba1()
        {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => Console.WriteLine($"{DateTime.Now} Kiba1 running");
        }
    
        public bool Start(HostControl hostControl)
        {
            _timer.Start();
            return true;
        }
         
        public bool Stop(HostControl hostControl)
        {
            _timer.Stop();
            return true;
        }
    }
    public class Program
    {
        public static void Main()                                
        {
            //新建服务 new
            HostFactory.New(x =>                                 
            {                                                    
                x.Service<Kiba1>();    //使用继承了ServiceControl的Kiba1类                                       
                x.RunAsLocalSystem();      //服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如:x.RunAs("username", "password");  x.RunAsPrompt(); x.RunAsNetworkService(); 等                      

                x.SetDescription("服务描述");           
                x.SetDisplayName("服务显示名");                  
                x.SetServiceName("服务名");                      
            }).Run();
  }
}

 github地址:https://github.com/kiba518/TopSelfDemo

posted @ 2020-04-10 11:10  kiba518  阅读(19)  评论(0编辑  收藏  举报