windows 服务开发组件之Topshelf
常用的windows服务开发有vs自带的windows服务开发,但是操作起来不是很便利,相比Topshelf后者更加的方便,且易于调试,安装,卸载.
1、安装
通过nuget搜索最新版本的topshelf,并安装到控制台项目(netcore和net均可)中,一般都是用控制台调试,然后通过topshelf安装成windows服务.
官方文档地址 组件支持的功能基本满足日常开发需求,如常规的服务描述,服务异常处理,服务恢复,服务启动模式等等不一一赘述,自行查阅文档.
2、代码
public static void Run<TExecutedService>(Action<TopshelfOptions> configurator) where TExecutedService : TopshelfService { var options=new TopshelfOptions(); configurator?.Invoke(options); if (string.IsNullOrEmpty(options.ServiceName)) throw new ArgumentNullException(nameof(options.ServiceName)); if (string.IsNullOrEmpty(options.DisplayName)) throw new ArgumentNullException(nameof(options.DisplayName)); HostFactory.Run(configurater => { configurater.Service<TExecutedService>(service => { service.ConstructUsing(hostSetting => { return ApplicationConfiguration.Current.Provider.GetRequiredService<TExecutedService>(); }); service.WhenStarted(s => s.Start()); service.WhenStopped(s => s.Stop()); }); configurater.RunAsLocalSystem(); configurater.SetDescription(options.Description?? options.DisplayName); configurater.SetDisplayName(options.DisplayName); configurater.SetServiceName(options.ServiceName); configurater.OnException(exception => { Console.WriteLine("业务执行异常,异常信息如下:" + exception.Message + "堆栈信息如下:" + exception.StackTrace); }); }); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2017-04-04 Oracle PL/SQL编程之变量
2017-04-04 Oracle PL/SQL编程之包(packages)
2017-04-04 Oracle PL/SQL编程之函数
2017-04-04 Oracle PL/SQL编程之过程