zoukankan      html  css  js  c++  java
  • C# 使用TopShelf实现Windows服务部署

    一、TopShelf介绍

        topShelf是创建Windows服务的一种方式,可以方便管理应用服务,最大广泛应用于,数据实时接收,定时推送等。

    二、TopShelf使用

        1、安装 TopShelf程序包

          

       2、编码 

     1 HostFactory.Run(x =>
     2             {
     3                 x.UseLog4Net();
     4                 x.Service<TownCrier>(s =>
     5                 {
     6                     s.ConstructUsing(name => new TownCrier());
     7                     s.WhenStarted(tc => tc.Start());
     8                     s.WhenStopped(tc => tc.Stop());
     9                 });
    10                 x.RunAsLocalSystem();
    11                 x.SetDescription("QuartzJob任务定时发送");
    12                 x.SetDisplayName("QuartzJob");
    13                 x.SetServiceName("QuartzJob");
    14 
    15                 x.EnablePauseAndContinue();
    16             });

           其中Run方法的回调参数方法,在运行时执行,方法内是一些配置信息。

           其中TownCrier类是我们服务实现类,服务启动时通过WhenStarted调用对应实现的OnStart,停止一样。

           RunAsLocalSystem()表示以本地系统账号运行,可选的还有网络服务和本地服务账号。

           SetDescription("Topshelf测试例子"); 设置服务的描述

           SetDisplayName("TopshelfExample"); 设置服务的显示名称

           x.SetServiceName("TopshelfExample"); 设置服务的名称

          TownCrier服务类:

     1 public class TownCrier
     2     {
     3         readonly Timer _timer;
     4         public TownCrier()
     5         {
     6             _timer = new Timer(1000) { AutoReset = true };
     7             _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("---------------------DateTime: {0} ------------------- ", DateTime.Now);
     8         }
     9         public void Start() { _timer.Start(); }
    10         public void Stop() { _timer.Stop(); }
    11     }

        3、安装

        使用管理员打开CMD窗口

        

         安装服务 server.exe install

         启动服务 server.exe start

         停止服务 server.exe stop

         卸载服务 server.exe uninstall

  • 相关阅读:
    Python学习第42天(ftp习题实现day4)
    Python学习第41天(ftp习题实现day3)
    Python学习第40天(ftp习题实现day2)
    Python学习第39天(ftp习题实现day1)
    Python学习第38天(文件处理,os、sys、pickle模块复习)
    Python学习第37天(socketserver继承原理、通讯加密)
    Python学习第36天(socketserver模块导入)
    Python学习第35天(粘包)
    个人读书笔记04—业务用例图
    命令模式
  • 原文地址:https://www.cnblogs.com/ZhengHengWU/p/11686087.html
Copyright © 2011-2022 走看看