Any way to override .NET Windows Service Name without recompiling?

前端 未结 5 2046
别跟我提以往
别跟我提以往 2021-01-29 23:39

I have a windows service executable that I know is written in .NET which I need to install under a different service name to avoid a conflict. The install doesn\'t provide anyw

5条回答
  •  长情又很酷
    2021-01-30 00:08

    1. Add project installer to your service
    2. Add method to get CustomService name

      private void RetrieveServiceName() 
      {
          var serviceName = Context.Parameters["servicename"];
          if (!string.IsNullOrEmpty(serviceName))
          {
              this.SomeService.ServiceName = serviceName;
              this.SomeService.DisplayName = serviceName;
          }
      }
      
    3. call on install and uninstall

      public override void Install(System.Collections.IDictionary stateSaver)
      {
         RetrieveServiceName();
        base.Install(stateSaver);
      }
      
      
      public override void Uninstall(System.Collections.IDictionary savedState)
      
      {
         RetrieveServiceName();
         base.Uninstall(savedState);
      }
      
    4. installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

    Source

提交回复
热议问题