【问题标题】:Multiple services in one assembly. How installer knows which service to install and start?一个组件中的多项服务。安装程序如何知道要安装和启动哪个服务?
【发布时间】:2011-05-17 22:27:32
【问题描述】:

我有一个包含 2 个 Windows 服务的项目。我创建了一个 ProjectInstaller 来安装这些项目,效果很好。但我有一个问题;鉴于下面定义的代码,项目安装程序如何知道要为 serviceInstaller1 安装哪个服务以及为 serviceInstaller2 安装哪个服务?

它只是基于 ServiceName 吗?

[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    private ServiceProcessInstaller process;
    private ServiceInstaller serviceInstaller1;
    private ServiceInstaller serviceInstaller2;
    public ProjectInstaller()
    {
        InitializeComponent();
        try
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            serviceInstaller1 = new ServiceInstaller();
            serviceInstaller1.ServiceName = "xxx";
            serviceInstaller1.Description = "Does Something";
            serviceInstaller1.StartType = ServiceStartMode.Automatic;

            serviceInstaller2 = new ServiceInstaller();
            serviceInstaller2.ServiceName = "yyy";
            serviceInstaller2.Description = "Does something else";
            serviceInstaller2.StartType = ServiceStartMode.Automatic;
            Installers.Add(process);
            Installers.Add(serviceInstaller1);
            Installers.Add(serviceInstaller2);
        }
        catch (Exception ex)
        {
            throw new Exception("Failed", ex);
        }
    }
}

【问题讨论】:

    标签: c# .net windows-services service-installer


    【解决方案1】:

    它基于ServiceName

    安装程序并不真正关心名称,您可以提供几乎任何名称,安装程序会很乐意为您注册具有此名称的 Windows 服务,但是当您尝试启动服务时,它将失败,除非它找到程序集中的服务,其 ServiceName 与安装程序中指定的 ServiceName 匹配。

    Error 1083: The executable program that this service is configured to run in does not implement the service.
    

    【讨论】:

    • 还有一个问题,当仅使用一个服务进行部署时,我假设名称无关紧要,因为它只是安装它找到的第一个服务。这是正确的吗?
    • @Mick Walker - 如果您在安装程序中有一个服务,并且您向 ServiceBase.Run() 方法提供了一个或多个服务,则无论服务名称如何,都将使用集合中的第一个服务。跨度>
    • 如果我需要从一个具有不同凭据的程序集创建两个服务怎么办?
    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多