使用Topshelf框架操作Windows服務(wù)
一、介紹
Topshelf是一個(gè)開(kāi)源的跨平臺(tái)的宿主服務(wù)框架,支持Windows和Mono,只需要幾行代碼就可以構(gòu)建一個(gè)很方便使用的服務(wù)宿主。
Topshelf是創(chuàng)建Windows服務(wù)的另一種方。它極大的簡(jiǎn)化服務(wù)創(chuàng)建與部署過(guò)程,它支持將控制臺(tái)應(yīng)用程序部署為服務(wù)。
下載
1、官網(wǎng):http://topshelf-project.com/ 這里面有詳細(xì)的文檔及下載
2、Topshelf的代碼托管在 http://github.com/topshelf/Topshelf/
二、使用
1、Topshelf 安裝
通過(guò) NuGet 安裝 Topshelf 包。
Install-Package Topshelf
2、Topshelf 配置
以下是我們以 Topshelf 來(lái)部署的一個(gè) gRPC 服務(wù)代碼,Topshelf 關(guān)鍵配置在 Main 方法內(nèi),更多的配置建議閱讀一下 官方文檔。
class Program { static void Main(string[] args) { // 配置和運(yùn)行宿主服務(wù) HostFactory.Run(x => { // 指定服務(wù)類型。這里設(shè)置為 CacheService x.Service<CacheService>(s => { s.ConstructUsing(name => new CacheService());// 通過(guò) new CacheService() 構(gòu)建一個(gè)服務(wù)實(shí)例 s.WhenStarted(tc => tc.Start());// 當(dāng)服務(wù)啟動(dòng)后執(zhí)行什么 s.WhenStopped(tc => tc.Stop());// 當(dāng)服務(wù)停止后執(zhí)行什么 }); x.RunAsLocalSystem();// 服務(wù)用本地系統(tǒng)賬號(hào)來(lái)運(yùn)行,身份標(biāo)識(shí),有好幾種方式,如:x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); 等 x.SetDescription("緩存服務(wù)");// 服務(wù)描述信息 x.SetDisplayName("CacheService");// 服務(wù)顯示名稱 x.SetServiceName("CacheService"); // 服務(wù)名稱 }); } } public class CacheService { private readonly string host = ConfigurationManager.AppSettings["Host"]; private readonly string port = ConfigurationManager.AppSettings["Port"]; readonly Server server; public CacheService() { server = new Server { Services = { MDCache.BindService(new CacheServiceImpl()) }, Ports = { new ServerPort(host, Convert.ToInt32(port), ServerCredentials.Insecure) } }; } public void Start() { server.Start(); } public void Stop() { server.ShutdownAsync(); } }
3、安裝服務(wù)
通過(guò)以上配置,確保程序集 Build 成功后,進(jìn)入 bin\Debug 目錄下,執(zhí)行 install 命令,一個(gè) Windows 服務(wù)就誕生了。(如果出現(xiàn)需要以管理員身份啟動(dòng)的提示,重新以管理員身份啟動(dòng) cmd )。
xxx.exe install
4、啟動(dòng)服務(wù)
啟動(dòng):
xxx.exe start
也可以安裝成功后我們可以在 Windows 服務(wù)下找到并啟動(dòng)它。
注意:因?yàn)?serviceName 必須是唯一的,如果我們希望在同一臺(tái)機(jī)器上運(yùn)行多個(gè)相同的服務(wù),那么我們需要注釋掉硬編碼設(shè)置的 ServiceName 和 DisplayName ,然后通過(guò)命令參數(shù)來(lái)動(dòng)態(tài)指定服務(wù)名稱。
// 服務(wù)顯示名稱 //x.SetDisplayName("CacheService"); // 服務(wù)名稱 //x.SetServiceName("CacheService");
xxx.exe install -servicename cacheService xxx.exe install -servicename cacheService1
5、服務(wù)卸載
卸載和啟動(dòng)的命令保持一致,只需要把 install 改成 uninstall 。
指定服務(wù)名稱卸載
三、Service Configuration 服務(wù)配置
以上為自定義模式,還有一種叫簡(jiǎn)單模式。繼承ServiceControl接口,實(shí)現(xiàn)該接口即可。
class Program { public static void Main(string[] args) { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x => { x.Service<TownCrier>(); x.RunAsLocalSystem(); x.SetDescription("Sample Topshelf Host服務(wù)的描述"); x.SetDisplayName("Stuff顯示名稱"); x.SetServiceName("Stuff服務(wù)名稱"); }); } } public class TownCrier : ServiceControl { private Timer _timer = null; readonly ILog _log = LogManager.GetLogger(typeof(TownCrier)); public TownCrier() { _timer = new Timer(1000) { AutoReset = true }; _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now); } public bool Start(HostControl hostControl) { _log.Info("TopshelfDemo is Started"); _timer.Start(); return true; } public bool Stop(HostControl hostControl) { throw new NotImplementedException(); } }
到此這篇關(guān)于使用Topshelf框架操作Windows服務(wù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中winform實(shí)現(xiàn)自動(dòng)觸發(fā)鼠標(biāo)、鍵盤(pán)事件的方法
這篇文章主要介紹了C#中winform實(shí)現(xiàn)自動(dòng)觸發(fā)鼠標(biāo)、鍵盤(pán)事件的方法,是C#程序設(shè)計(jì)中非常實(shí)用的功能,需要的朋友可以參考下2014-08-08C# 無(wú)邊框窗體之窗體移動(dòng)實(shí)現(xiàn)代碼
這篇文章介紹了C# 無(wú)邊框窗體之窗體移動(dòng)實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-10-10C# wpf使用ListBox實(shí)現(xiàn)尺子控件的示例代碼
本文主要介紹了C# wpf使用ListBox實(shí)現(xiàn)尺子控件的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C# 中文簡(jiǎn)體轉(zhuǎn)繁體實(shí)現(xiàn)代碼
C# 中文簡(jiǎn)體轉(zhuǎn)繁體實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02C# 運(yùn)用params修飾符來(lái)實(shí)現(xiàn)變長(zhǎng)參數(shù)傳遞的方法
一般來(lái)說(shuō),參數(shù)個(gè)數(shù)都是固定的,定義為集群類型的參數(shù)可以實(shí)現(xiàn)可變數(shù)目參數(shù)的目的,但是.NET提供了更靈活的機(jī)制來(lái)實(shí)現(xiàn)可變數(shù)目參數(shù),這就是使用params修飾符2013-09-09C#利用Openxml讀取Excel數(shù)據(jù)實(shí)例
這篇文章主要介紹了C#利用Openxml讀取Excel數(shù)據(jù)的方法,包括使用中的注意點(diǎn)分析及疑難探討,需要的朋友可以參考下2014-09-09c#實(shí)現(xiàn)KTV點(diǎn)歌系統(tǒng)
這篇文章主要用C#語(yǔ)言編寫(xiě)的KTV點(diǎn)歌系統(tǒng),需要的朋友可以參考下2015-07-07在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01