03.01 「實戰」如何根據自己業務自定義配置Topshelf 服務

前面講了如何使用Topshelf 快速開發windows服務, 不清楚的可以看之前的這篇文章:https://www.cnblogs.com/zhangweizhong/category/771057.html,

今天說一說Topshelf 的相關配置。

簡單配置

官方文檔,對HostFactory 裡面的參數做了詳細的說明:http://docs.topshelf-project.com/en/latest/configuration/config_api.html ,下面只對一些常用的方法進行簡單的解釋:

我們將上面的程序代碼改一下:

<code>            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服務名稱");                       //服務名稱            });    /<towncrier>/<code>

重裝安裝運行後:

「實戰」如何根據自己業務自定義配置Topshelf 服務

通過上面,相信大家都很清楚 SetDescription、SetDisplayName、SetServiceName區別。不再細說。

服務配置

Topself的服務一般有主要有兩種使用模式。

一、簡單模式。繼承ServiceControl接口,實現該接口即可。

「實戰」如何根據自己業務自定義配置Topshelf 服務

實例:

<code>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("Stuff服務名稱");            });        }    }}/<towncrier>/<code>

二、常用模式。

「實戰」如何根據自己業務自定義配置Topshelf 服務

實例:

<code>namespace TopshelfDemo {    public class TownCrier {        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 void Start()        {            _timer.Start();        }        public void Stop()        {            _timer.Stop();        }    }    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>(s => {                s.ConstructUsing(name => new TownCrier());                s.WhenStarted(tc => tc.Start());                s.WhenStopped(tc => tc.Stop()); });                x.RunAsLocalSystem();                x.SetDescription("Sample Topshelf Host服務的描述");                x.SetDisplayName("Stuff顯示名稱");                x.SetServiceName("Stuff服務名稱");            });        }    }}/<towncrier>/<code>

兩種方式,都使用了Log4Net,相關配置:

<code> xml version="1.0" encoding="utf-8" ?><configuration>  <configsections>      /<configsections>  <log4net>    <appender>            <param>            <param>            <param>            <param>            <param>            <param>      <layout>        <param>      /<layout>    /<appender>        <appender>      <mapping>        <level>        <forecolor>      /<mapping>      <mapping>        <level>        <forecolor>      /<mapping>      <layout>        <conversionpattern>      /<layout>      <filter>        <param>        <param>      /<filter>    /<appender>    <root>            <level>      <appender-ref>      <appender-ref>    /<root>  /<log4net>/<configuration>/<code>

推薦使用第二種常用模式。


最後

以上就把如何根據自己需要配置Topshelf 服務講完了,大家可以根據自己的需要自定義配置定時任務。


分享到:


相關文章: