標籤: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服務