最近遇到一个问题,就是新建了一个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

      

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2022-09-12 C#小技巧---递归优化的三种方式
2022-09-12 C#小技巧---Linq的使用

随笔 - 88, 文章 - 0, 评论 - 18, 阅读 - 47077

Copyright © 2025 樱木007
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示