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

2、Topshelf 配置
以下是我們以 Topshelf 來部署的一個 gRPC 服務代碼,Topshelf 關鍵配置在 Main 方法內,更多的配置建議閱讀一下 官方文檔。
class Program
{
static void Main(string[] args)
{
// 配置和運行宿主服務
HostFactory.Run(x =>
{
// 指定服務類型。這里設置為 CacheService
x.Service<CacheService>(s =>
{
s.ConstructUsing(name => new CacheService());// 通過 new CacheService() 構建一個服務實例
s.WhenStarted(tc => tc.Start());// 當服務啟動后執(zhí)行什么
s.WhenStopped(tc => tc.Stop());// 當服務停止后執(zhí)行什么
});
x.RunAsLocalSystem();// 服務用本地系統(tǒng)賬號來運行,身份標識,有好幾種方式,如:x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); 等
x.SetDescription("緩存服務");// 服務描述信息
x.SetDisplayName("CacheService");// 服務顯示名稱
x.SetServiceName("CacheService"); // 服務名稱
});
}
}
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、安裝服務
通過以上配置,確保程序集 Build 成功后,進入 bin\Debug 目錄下,執(zhí)行 install 命令,一個 Windows 服務就誕生了。(如果出現需要以管理員身份啟動的提示,重新以管理員身份啟動 cmd )。
xxx.exe install

4、啟動服務
啟動:
xxx.exe start
也可以安裝成功后我們可以在 Windows 服務下找到并啟動它。

注意:因為 serviceName 必須是唯一的,如果我們希望在同一臺機器上運行多個相同的服務,那么我們需要注釋掉硬編碼設置的 ServiceName 和 DisplayName ,然后通過命令參數來動態(tài)指定服務名稱。
// 服務顯示名稱
//x.SetDisplayName("CacheService");
// 服務名稱
//x.SetServiceName("CacheService");xxx.exe install -servicename cacheService xxx.exe install -servicename cacheService1

5、服務卸載
卸載和啟動的命令保持一致,只需要把 install 改成 uninstall 。

指定服務名稱卸載

三、Service Configuration 服務配置
以上為自定義模式,還有一種叫簡單模式。繼承ServiceControl接口,實現該接口即可。
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服務的描述");
x.SetDisplayName("Stuff顯示名稱");
x.SetServiceName("Stuff服務名稱");
});
}
}
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();
}
}到此這篇關于使用Topshelf框架操作Windows服務的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#中winform實現自動觸發(fā)鼠標、鍵盤事件的方法
這篇文章主要介紹了C#中winform實現自動觸發(fā)鼠標、鍵盤事件的方法,是C#程序設計中非常實用的功能,需要的朋友可以參考下2014-08-08
在C# WPF下自定義滾動條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動條ScrollViewer樣式的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

