首页 热点 业界 科技快讯 数码 电子消费 通信 前沿动态 电商

最新消息:使用Topshelf框架操作Windows服务

2022-06-13 09:50:16 来源 : 软件开发网

一、介绍

Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。

Topshelf是创建Windows服务的另一种方。它极大的简化服务创建与部署过程,它支持将控制台应用程序部署为服务。

下载

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());// 当服务启动后执行什么 s.WhenStopped(tc => tc.Stop());// 当服务停止后执行什么 }); x.RunAsLocalSystem();// 服务用本地系统账号来运行,身份标识,有好几种方式,如: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 目录下,执行 install 命令,一个 Windows 服务就诞生了。(如果出现需要以管理员身份启动的提示,重新以管理员身份启动 cmd )。

(资料图片)

xxx.exe install
4、启动服务

启动:

xxx.exe start

也可以安装成功后我们可以在 Windows 服务下找到并启动它。

注意:因为 serviceName 必须是唯一的,如果我们希望在同一台机器上运行多个相同的服务,那么我们需要注释掉硬编码设置的 ServiceName 和 DisplayName ,然后通过命令参数来动态指定服务名称。

// 服务显示名称//x.SetDisplayName("CacheService");// 服务名称//x.SetServiceName("CacheService");
xxx.exe install -servicename cacheServicexxx.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服务的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。

标签: 支持软件 必须是唯一的 还有一种

相关文章

最近更新
观焦点:超萌相机 2023-03-01 12:29:37
海南百货网 2023-03-01 12:13:44
焦点热讯:宜点充 2023-02-28 10:10:16
天天关注:小铺CEO 2023-02-28 10:07:13
【世界聚看点】KaFit 2023-02-28 09:31:37
葱天下 2023-02-28 09:17:03
渔界竞钓 2023-02-28 08:15:29
焦点快看:鲸奇视频 2023-02-28 06:30:37
环球热议:萝小逗 2023-02-27 23:25:49
简讯:小码公交 2023-02-27 23:16:12
彼岸花 2023-02-27 22:32:52
时时夺宝 2023-02-27 21:37:50
天天动态:袜之源 2023-02-27 21:29:50
天天资讯:AI空气 2023-02-27 20:19:46
世界时讯:绘读 2023-02-27 20:19:41
看点:一元得购 2023-02-27 19:26:28