Quartz.NET总结(八)如何根据自己需要配置Topshelf 服务

 前面讲了如何使用Topshelf 快速开发windows服务, 不清楚的可以看之前的这篇文章:

            HostFactory.Run(x =>                                 //1            {                 x.Service<TownCrier>(s =>                        //2                {                     s.ConstructUsing(name => new TownCrier());     //配置一个完全定制的服务,对Topshelf没有依赖关系。常用的方式。             //the start and stop methods for the service                    s.WhenStarted(tc => tc.Start());              //4                    s.WhenStopped(tc => tc.Stop());               //5                });                 x.RunAsLocalSystem();                            // 服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如:x.RunAs("username", "password");  x.RunAsPrompt(); x.RunAsNetworkService(); 等                 x.SetDescription("Sample Topshelf Host服务的描述");        //安装服务后,服务的描述                x.SetDisplayName("Stuff显示名称");                       //显示名称                x.SetServiceName("Stuff服务名称");                       //服务名称            });    
复制代码

 

重装安装运行后:

Quartz.NET总结(八)完整的Topshelf 服务配置

 

通过上面,相信大家都很清楚 SetDescription、SetDisplayName、SetServiceName区别。不再细说。

服务配置

Topself的服务一般有主要有两种使用模式。

一、简单模式。继承ServiceControl接口,实现该接口即可。

Quartz.NET总结(八)完整的Topshelf 服务配置

 

实例:

复制代码
namespace TopshelfDemo {     public class TownCrier : ServiceControl     {         private Timer _timer = null;         readonly ILog _log = LogManager.GetLogger(typeof(TownCrier));         public TownCrier()         {             _timer = new Timer(1000) { AutoReset = true };             _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);         }          public bool Start(HostControl hostControl)         {             _log.Info("TopshelfDemo is Started");             _timer.Start();             return true;         }          public bool Stop(HostControl hostControl)         {             throw new NotImplementedException();         }     }     class Program {         public static void Main(string[] args)         {             var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");             XmlConfigurator.ConfigureAndWatch(logCfg);             HostFactory.Run(x => {                 x.Service<TownCrier>();                 x.RunAsLocalSystem();                 x.SetDescription("Sample Topshelf Host服务的描述");                 x.SetDisplayName("Stuff显示名称"); x.SetServiceName("
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信