c# – Windows服务中的一个ServiceProcessInstaller中的多个ServiceInstaller

我在ServiceProcessInstaller中添加了两个ServiceInstallers.之后我改变了我的Main()如下:

static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            new Service1(),
            new Service2()                
        };
        ServiceBase.Run(ServicesToRun);
    }

我还在Service1上将Service2设置为依赖服务,如下所示:

 private void InitializeComponent()
    {
        this.Service1ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
        this.Service1Installer = new System.ServiceProcess.ServiceInstaller();
        this.Service2Installer = new System.ServiceProcess.ServiceInstaller();
        // 
        // Service1ProcessInstaller
        // 
        this.Service1ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        this.Service1ProcessInstaller.Password = null;
        this.Service1ProcessInstaller.Username = null;
        // 
        // Service1Installer
        // 
        this.Service1Installer.ServiceName = "Service1";
        this.Service1Installer.ServicesDependedOn = new string[] {"Service2"};
        // 
        // Service2Installer
        // 
        this.Service2Installer.ServiceName = "Service2";
        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.Service1ProcessInstaller,
        this.Service1Installer,
        this.Service2Installer});

    }

它仍然只运行我的Service1.

Service2永远不会打电话.

如果我在Main()中更改序列,则Service2仅调用.

它总是呼叫第一个服务.

如何拨打我的两项服务?

最佳答案 我找到了解决方案.问题不在于卸载时的依赖服务.我已经卸载了我的服务然后再次安装它然后我在我的Services.msc中找到了这两个服务.

当它们实际上彼此依赖时我们需要依赖服务,所以我也删除了相关的服务代码.

现在我可以手动启动它们.它们都在运行.下面是我成功运行的代码.

static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    {
        new Service1(),
        new Service2()                
    };
    ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
    this.Service1ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
    this.Service1Installer = new System.ServiceProcess.ServiceInstaller();
    this.Service2Installer = new System.ServiceProcess.ServiceInstaller();
    // 
    // Service1ProcessInstaller
    // 
    this.Service1ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.Service1ProcessInstaller.Password = null;
    this.Service1ProcessInstaller.Username = null;
    // 
    // Service1Installer
    // 
    this.Service1Installer.ServiceName = "Service1";
    // 
    // Service2Installer
    // 
    this.Service2Installer.ServiceName = "Service2";
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
    this.Service1ProcessInstaller,
    this.Service1Installer,
    this.Service2Installer});

}
点赞