【问题标题】:Any way to override .NET Windows Service Name without recompiling?有什么方法可以在不重新编译的情况下覆盖 .NET Windows 服务名称?
【发布时间】:2010-12-14 20:13:53
【问题描述】:

我有一个 Windows 服务可执行文件,我知道它是用 .NET 编写的,我需要以不同的服务名称安装它以避免冲突。安装无论如何都不提供指定服务名称。如果我只能访问二进制文件,那么当我使用 installutil 安装它时是否可以覆盖服务名称?

【问题讨论】:

    标签: .net windows-services


    【解决方案1】:

    您必须使用 InstallUtil 吗?以下是使用 sc 执行所需操作的命令:

    sc create MyService binPath= "MyService.exe" DisplayName= "MyService"  
    sc description MyService "My description"
    

    参考:http://support.microsoft.com/kb/251192

    【讨论】:

    • 这看起来正是我想要的——但是我无法让它工作。我只是不断收到“使用”消息。
    • 我的问题是等号和 binPath 值之间显然 必须 有一个空格,例如sc create ahSchedulerService binPath="MyService.exe",而不是 sc create ahSchedulerService binPath="MyService.exe"。
    • 啊,我忘了。很抱歉给你一个不好的例子。
    • 当我使用SC命令创建服务实例时,我发现我必须将整个路径放在EXE名称之前。在运行 SC 之前,我将命令提示符目录更改为与 EXE 相同,以为这样就足够了,但事实并非如此。当我尝试启动服务时,我收到一条错误消息“系统找不到指定的文件”。所以 SC 命令必须有一个参数,如: binPath= "C:\whatever\servieName.exe"
    【解决方案2】:

    InstallUtil 不允许您配置服务名称是不正确的。我一直都是这样的

    InstallUtil.exe /servicename="<service name>" "<path to service exe>"
    

    【讨论】:

    • 如果你已经有一个与exe同名的服务,它会报错System.ComponentModel.Win32Exception: The specified service already exists。我试图安装 2 个相同服务的实例并以不同的方式命名它们。使用以下答案中给出的 sc create 方法
    • 不起作用。给出我的服务已经存在的错误。
    • 如果您有项目安装程序并覆盖安装和卸载,如@Volodymyrs answer stackoverflow.com/a/25259719/169714
    • 不适用于从 Visual Studio 创建的默认安装程序
    • 如果我将参数“servicename”更改为“name”,它对我有用。
    【解决方案3】:
    1. 将项目安装程序添加到您的服务中
    2. 添加获取自定义服务名称的方法

      private void RetrieveServiceName() 
      {
          var serviceName = Context.Parameters["servicename"];
          if (!string.IsNullOrEmpty(serviceName))
          {
              this.SomeService.ServiceName = serviceName;
              this.SomeService.DisplayName = serviceName;
          }
      }
      
    3. 调用安装和卸载

      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

    【讨论】:

    • 这非常有用,我确实必须在添加此代码后重新编译我的服务可执行文件才能使其工作,这对我来说不是问题。
    • 取自源链接,但以防万一...您需要将“someService”更改为 ServiceInstaller 名称。它可能是 ServiceInstaller1,因为这是默认设置。
    【解决方案4】:

    这对我很有效!

    我希望有人可以使用它。

    【讨论】:

    • 图像损坏
    【解决方案5】:

    尝试使用 sc.exe 安装您的服务。快速搜索将产生大量文档。使用该工具可以轻松修改现有服务和/或添加新服务——包括名称。

    编辑:我用这个工具安装我的 .NET 服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 2013-12-31
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多