使用Topshelf建立Windows服務

來源:互聯網
上載者:User

標籤:支援   esc   分享圖片   script   splay   卸載   web   ice   初始化   

Topshelf是一個開源的跨平台的服務架構,支援Windows和Mono,只需要幾行代碼就可以構建一個很方便使用的服務. 官方網站:http://topshelf-project.com

下面是將普通控制台建立成Windows服務:

1.引用程式集TopShelf.dll

  通過NuGet安裝或者官網下載

2.建立ApiMain類

 

 public class APIMain    {        public APIMain()        {            //初始化方法,可以不要        }        public void Start()        {            //服務啟動,執行的內容        }        public void Stop()        {            //服務停止,執行的內容        }    }

 

3.改寫Program.cs

 public class Program    {        static void Main(string[] args)        {            HostFactory.Run(x =>            {                x.Service<APIMain>(s =>                    {                        s.ConstructUsing(name => new APIMain());                        s.WhenStarted(tc => tc.Start()); //啟動服務執行方法                        s.WhenStopped(tc => tc.Stop()); //停止服務前執行方法                    });                x.RunAsLocalSystem();                x.SetDescription("資料介面服務"); //服務描述名稱                x.SetDisplayName("資料介面服務"); //服務顯示名稱                x.SetServiceName("ElegantWebApiServer");            });        }    }

4.cmd命令安裝或卸載服務

 

安裝:xxx.exe  install    

 

卸載:xxx.exe  uninstall 

 

 

 

  

使用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.