4

我已将两个 ServiceInstaller 添加到我的 ServiceProcessInstaller。之后我改变了我的 Main() 如下:

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

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

 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 只会调用。

它总是调用第一个服务。

如何调用我的两种服务?

4

1 回答 1

1

我找到了解决方案。问题不在于依赖服务,而在于我的卸载。我已经卸载了我的服务,然后再次安装它,然后我在我的 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});

}
于 2017-01-07T06:25:10.270 回答