.NET开发Windows Service程序 - Topshelf
生活随笔
收集整理的這篇文章主要介紹了
.NET开发Windows Service程序 - Topshelf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在實際項目開發過程中,會經常寫一些類似定時檢查,應用監控的應用。這類應用在windows平臺通常都會寫成window service程序。
在百度上搜索一下'c#開發windows service',基本都是使用VS windows服務的模板來開發,使用VS Attach到服務的進程來調試,使用InstallUtil工具來安裝windows服務。還是有些麻煩的。
本文主要介紹一下使用Topshelf開源框架來創建windows服務,簡單、方便和快捷。官方文檔也很簡單,花半個小時過一遍即可https://topshelf.readthedocs.io/en/latest/configuration/quickstart.html
創建一個控制臺程序,安裝Topshelf到工程中
Install-Package Topshelf Install-Package Topshelf.NLog // for Logging程序示例
using System; using System.Timers; using Topshelf;namespace TopshelfFirstDemo {public class TownCrier{readonly Timer _timer;public TownCrier(){_timer = new Timer(1000) { 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(); }}class Program{static int Main(string[] args){var exitCode = HostFactory.Run(x =>{x.UseNLog();x.Service<TownCrier>(s =>{s.ConstructUsing(name => new TownCrier());s.WhenStarted(tc => tc.Start());s.WhenStopped(tc => tc.Stop());});x.RunAsLocalSystem();x.SetDescription("A test service using Topshelf");x.SetDisplayName("TestTopshelf");x.SetServiceName("TestTopshelf");x.OnException(ex =>{// Logging});});Console.WriteLine(exitCode);return (int)exitCode;}} }可通過設置Service各種參數來設置開發的windows service,關于Service的參數配置,請參考官方文檔。
安裝windows service
CD到示例程序Debug/Release bin目錄
列出各命令
TopshelfFirstDemo.exe help安裝服務
TopshelfFirstDemo.exe install卸載服務
TopshelfFirstDemo.exe uninstallstart和stop服務的方式有很多了,通過
?
轉載于:https://www.cnblogs.com/codesee/p/6242154.html
總結
以上是生活随笔為你收集整理的.NET开发Windows Service程序 - Topshelf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Idris趋近发布1.0版
- 下一篇: .NET开发作业调度(job sched