一、官方網站及源碼下載
1、官方網站:
http://topshelf-project.com/
二、Topshelf優勢
1、調試
方便
:不用創建windows服務項目,直接創建控制台程序即可,啟動控制台就可以進行服務代碼調試
2、安裝/卸載服務方法
1、cmd-->cd 程序目錄(直接定位到exe文件所在目錄)
1、cmd-->cd 程序目錄(直接定位到exe文件所在目錄)
windows 7 cd C:
Widows Serve cd /d C:
2、安裝服務:JwifiRoute.Message.LogServices.exe install
3、啟動服務: JwifiRoute.Message.LogServices.exe start
3、啟動服務: JwifiRoute.Message.LogServices.exe start
4、卸載服務(需要執行多次才能卸載服務):JwifiRoute.Message.LogServices.exe uninstall
三、使用Topshelf創建服務
1、引入Topshelf.dll
2、啟動服務
/// <summary> /// 應用程序的主入口點。 /// </summary> static void Main() { HostFactory.Run(c => { c.SetServiceName("LogServices"); c.SetDisplayName("LogServices"); c.SetDescription("LogServices"); c.Service<TopshelfService>(s => { s.ConstructUsing(b => new TopshelfService()); s.WhenStarted(o => o.Start()); s.WhenStopped(o => o.Stop()); }); });
3、服務程序邏輯
public class TopshelfService { public void Start() { //服務邏輯 } public void Stop() { } }