使用Topshelf建立Windows服務

來源:互聯網
上載者:User

標籤:void   rgs   eset   ado   factor   load   github   des   ref   

1.Topshelf是建立Windows服務的另一種方法,Topshelf是一個開源的跨平台的宿主服務架構,支援Windows和Mono,只需要幾行代碼就可以構建一個很方便使用的服務宿主。

2.添加引用:Topshelf.dll

      Topshelf.Log4Net.dll

3.最新版本:https://github.com/Topshelf/Topshelf/downloads

4.官方案例:

class Program    {        public static void Main()        {            HostFactory.Run(x =>                                 //1            {                x.Service<TownCrier>(s =>                        //2                {                    s.ConstructUsing(name => new TownCrier());     //3                    s.WhenStarted(tc => tc.Start());              //4                    s.WhenStopped(tc => tc.Stop());               //5                });                x.RunAsLocalSystem();                            //6                x.SetDescription("Sample Topshelf Host");        //7                x.SetDisplayName("Stuff");                       //8                x.SetServiceName("Stuff");                       //9            });                                                  //10        }    }    public class TownCrier    {        readonly Timer _timer;        public TownCrier()        {            _timer = new Timer(5000) { AutoReset = true };            _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);        }        public void Start() { _timer.Start(); }        public void Stop() { _timer.Stop(); }    }

5.安裝卸載方法:

安裝:TopshelfDemo.exe install

啟動:TopshelfDemo.exe start

卸載:TopshelfDemo.exe uninstall 6.安裝完成後即可在,Windows服務裡看到 

使用Topshelf建立Windows服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.