最近遇到一个问题,就是新建了一个windows服务,然后需要在同一个服务器上部署两个实例(服务名称不一样,使用的执行码一样),刚开始以为直接在部署时设置参数服务名称不一致就可以,然后部署第二次的时候就报错,最后查了半天,找到了解决办法,文章中有好几种解决办法,我觉得最实用的解决方法记录一下
为ServiceName
和DisplayName
指定自定义值的快速方法是使用installutil
命令行参数。
在 ProjectInstaller
类中覆盖虚拟方法 Install(IDictionary stateSaver)
和 Uninstall(IDictionary savedState)
在ProjectInstaller.Designer.cs中加入以下代码
1 public override void Install(System.Collections.IDictionary stateSaver) 2 { 3 GetCustomServiceName(); 4 base.Install(stateSaver); 5 } 6 public override void Uninstall(System.Collections.IDictionary savedState) 7 { 8 GetCustomServiceName(); 9 base.Uninstall(savedState); 10 } 11 private void GetCustomServiceName() 12 { 13 string customServiceName = Context.Parameters["servicename"]; 14 if (!string.IsNullOrEmpty(customServiceName)) 15 { 16 serviceInstaller1.ServiceName = customServiceName; 17 serviceInstaller1.DisplayName = customServiceName; 18 } 19 }
安装服务时使用命令
installutil.exe /servicename="CustomServiceName" "c:\pathToService\SrvcExecutable.exe"
其他解决方法请详见下面链接
参考链接:https://www.u72.net/b/show-317328.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2022-09-12 C#小技巧---递归优化的三种方式
2022-09-12 C#小技巧---Linq的使用