使用Topshelf管理Windows服务

使用Topshelf管理Windows服务

 
 
一、官方网站及源码下载
      1、官方网站:http://topshelf-project.com/
 
二、Topshelf优势
      1、调试方便:不用创建windows服务项目,直接创建控制台程序即可,启动控制台就可以进行服务代码调试
      2、安装/卸载服务方法
           1、cmd-->cd 程序目录(直接定位到exe文件所在目录)
      windows 7   cd C:
             Widows Serve  cd /d C:
           2、安装服务:JwifiRoute.Message.LogServices.exe install
           3、启动服务:JwifiRoute.Message.LogServices.exe start
           4、卸载服务(需要执行多次才能卸载服务):JwifiRoute.Message.LogServices.exe uninstall
 
三、使用Topshelf创建服务
       1、引入Topshelf.dll
       2、启动服务
         
复制代码
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            HostFactory.Run(c =>
            {
                c.SetServiceName("LogServices");
                c.SetDisplayName("LogServices");
                c.SetDescription("LogServices");

                c.Service<TopshelfService>(s =>
                {
                    s.ConstructUsing(b => new TopshelfService());
                    s.WhenStarted(o => o.Start());
                    s.WhenStopped(o => o.Stop());
                });
            });
复制代码
          3、服务程序逻辑
复制代码
public class TopshelfService
      {
        public void Start()
        {
            //服务逻辑
        }

 public void Stop()
        {
        }
}
复制代码
原文地址:https://www.cnblogs.com/zxtceq/p/7891297.html