【问题标题】:Error while trying to run multiple instances of a windows service尝试运行 Windows 服务的多个实例时出错
【发布时间】:2017-03-21 06:42:10
【问题描述】:

我试图在同一台服务器上运行服务的多个实例以支持多个环境。

我尝试按照推荐的herehere 修改ServiceInstaller.ServiceName。

但是,MSDN says “ServiceName 必须与 [Service] 类的 ServiceBase.ServiceName 相同。”

忽略 MSDN,在看到上面链接的两个页面后,我还是尝试这样做。但是,当我这样做时,我收到一条错误消息:“Windows 无法在本地计算机上启动 {Display Name} 服务。错误 1083:此服务配置为在其中运行的可执行程序未实现该服务。”

This answer 确认这是该错误的解释。

【问题讨论】:

    标签: .net windows-services


    【解决方案1】:

    这件事困扰了我很久。然后,当我第 5 次阅读 this Q&A 时,我被击中了。

    那篇文章没有使用动态名称,但我注意到它在 Main 函数中设置了 ServiceBase.ServiceName。我突然想到,也许我可以动态地做同样的事情,而且它奏效了。

    我的主要功能现在看起来像这样:

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1(), 
                new Service2(),
                new Service3()
            };
    
            foreach (serviceBase Service in ServicesToRun)
            {
                service.ServiceName = FunctionToComputeDynamicName(service);
            }
            ServiceBase.Run(ServicesToRun);
        }
    

    我在安装时从 Installer.Context 获取名称,在运行时从 App.config 获取名称。如果您愿意,还可以使用 Main 的 Args[] 版本轻松获取运行时名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多