使用Topshelf框架操作Windows服務(wù)
一、介紹
Topshelf是一個(gè)開源的跨平臺(tái)的宿主服務(wù)框架,支持Windows和Mono,只需要幾行代碼就可以構(gòu)建一個(gè)很方便使用的服務(wù)宿主。
Topshelf是創(chuàng)建Windows服務(wù)的另一種方。它極大的簡化服務(wù)創(chuàng)建與部署過程,它支持將控制臺(tái)應(yīng)用程序部署為服務(wù)。
下載
1、官網(wǎng):http://topshelf-project.com/ 這里面有詳細(xì)的文檔及下載
2、Topshelf的代碼托管在 http://github.com/topshelf/Topshelf/
二、使用
1、Topshelf 安裝
通過 NuGet 安裝 Topshelf 包。
Install-Package Topshelf

2、Topshelf 配置
以下是我們以 Topshelf 來部署的一個(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());// 通過 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)來運(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ù)
通過以上配置,確保程序集 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 ,然后通過命令參數(shù)來動(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ù)配置
以上為自定義模式,還有一種叫簡單模式。繼承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# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法
本文將和大家介紹 MemoryMarshal 輔助類,通過這個(gè)輔助類用來實(shí)現(xiàn)結(jié)構(gòu)體數(shù)組和二進(jìn)制數(shù)組的相互轉(zhuǎn)換,對(duì)C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法感興趣的朋友一起看看吧2023-09-09
C#簡單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#簡單嵌套flash讀取數(shù)據(jù)的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11
C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類
這篇文章主要給大家介紹了關(guān)于C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09
C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法
一般情況下,在標(biāo)題欄中按住鼠標(biāo)左鍵不放即可實(shí)現(xiàn)拖動(dòng)操作,當(dāng)做浮動(dòng)窗體時(shí),如果包含窗體邊框,那么界面給使用者的感覺將很不友好,因此本文給大家介紹了C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法,感興趣的朋友可以參考下2024-04-04
C#中async await異步關(guān)鍵字用法和異步的底層原理全解析
這篇文章主要介紹了C#中async await異步關(guān)鍵字用法和異步的底層原理全解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-04-04

