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

      

Copyright © 2024 樱木007
Powered by .NET 8.0 on Kubernetes