有没有重新编译覆盖.NET Windows服务名称的方法?

Nat*_*han 70 .net windows-services

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

Jos*_*ger 102

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

sc create MyService binPath= "MyService.exe" DisplayName= "MyService"  
sc description MyService "My description"
Run Code Online (Sandbox Code Playgroud)

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

  • 我的问题是,显然*必须*是等号和binPath值之间的空格,例如sc create ahSchedulerService binPath ="MyService.exe",而不是sc create ahSchedulerService binPath ="MyService.exe". (33认同)

Sac*_*nth 29

InstallUtil不允许您配置服务名称.我一直这样做

InstallUtil.exe /servicename="<service name>" "<path to service exe>"
Run Code Online (Sandbox Code Playgroud)

  • 如果你已经有一个与exe同名的服务,它将给出错误`System.ComponentModel.Win32Exception:指定的服务已经存在`.我试图安装2个相同服务的实例并以不同方式命名.使用以下答案中给出的sc create方法 (6认同)
  • 不行.给出我的服务已存在的错误. (4认同)
  • 即使使用具有管理员权限的cmd,也不适用于我. (3认同)
  • 如果你有一个项目安装程序并覆盖安装和卸载,就像在@Volodymyrs中一样回答http://stackoverflow.com/a/25259719/169714 (2认同)

Vol*_*hat 24

  1. 添加项目安装程序到您的服务
  2. 添加方法以获取CustomService名称

    private void RetrieveServiceName() 
    {
        var serviceName = Context.Parameters["servicename"];
        if (!string.IsNullOrEmpty(serviceName))
        {
            this.SomeService.ServiceName = serviceName;
            this.SomeService.DisplayName = serviceName;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

资源


kul*_*nja 5

在此输入图像描述

这对我来说非常有用!

我希望有人可以使用它.