Topshelf如何支援Mono擴充Topshelf.Linux

來源:互聯網
上載者:User

使用Topshelf 5步建立Windows 服務 這篇文章大家可以瞭解到使用Topshelf可以很好的支援Windows服務的開發,但是它和Mono不相容,Github上有一個擴充https://github.com/pruiz/Topshelf.Linux 可以很好相容Linux/Mono,本文介紹使用方法:

1、在項目中添加Topshelf.Linux, 通過Nuget 很方便的添加引用:

2、在代碼中加入下面一行代碼UseLinuxIfAvailable() :

class Program  {      static ILog _log = LogManager.GetLogger(typeof(Program));          static void Main(string[] args)      {          System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);          XmlConfigurator.ConfigureAndWatch(              new FileInfo("log4net.config"));              var host = HostFactory.New(x =>          {              x.Service<SampleService>(s =>              {                  s.ConstructUsing(() => new SampleService());                  s.WhenStarted(v => v.Start());                  s.WhenStopped(v => v.Stop());                  });              x.UseLinuxIfAvailable();               x.RunAsLocalSystem();              x.UseLog4Net();              x.SetDescription("SampleService Description");              x.SetDisplayName("SampleService");              x.SetServiceName("SampleService");          });          host.Run();      }

這樣你的基於Topshelf Windows服務就完成了相容Mono的改造工作。但是要注意的是在Mono下支援命令列運行,不能使用Topshelf的命令列Start,Stop控制服務等,這對於Linux環境來說足夠了,可以通過rc-scripts來完成這些工作。

本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.