在同一个应用程序中启动两个Windows服务

时间:2016-08-17 12:47:30

标签: c# .net wpf windows service

我有以下问题。

我有两个Windows服务应用程序在同一个应用程序(WPF)中启动,两个都已安装。但是当我必须“启动”它们(ServiceController.Start())时,只有先启动的服务保持打开状态,另一个返回消息:

  

错误1053:服务未响应启动或控制   及时提出要求

这是我启动它们的代码:

//*TS_TimeOut timespan has 1 hour
    private void InitServiceOne()
    {
        ServiceController SControllerServiceOne = new ServiceController("ServiceOne", Environment.MachineName);

        SControllerServiceOne.Start();
        SControllerServiceOne.WaitForStatus(ServiceControllerStatus.Running, TS_TimeOut);
    }

    private void InitServiceTwo()
    {

        ServiceController SControllerServiceTwo = new ServiceController("ServiceTwo", Environment.MachineName);

        SControllerServiceTwo.Start();
        SControllerServiceTwo.WaitForStatus(ServiceControllerStatus.Running, TS_TimeOut);
    }

这是我用来安装它们的代码:

    private void InstallServiceOne(string strServiceOnePath)
    {
        ServiceProcessInstaller ProcessServiceServiceOne = new ServiceProcessInstaller();

        ServiceInstaller ServiceInstallerOne = new ServiceInstaller();
        InstallContext Context = new System.Configuration.Install.InstallContext();
        String path = String.Format("/assemblypath={0}", strServiceOnePath));
        String[] cmdline = { path };

        Context = new System.Configuration.Install.InstallContext("", cmdline);
        ServiceInstallerOne.Context = Context;
        ServiceInstallerOne.DisplayName = "ServiceOne";
        ServiceInstallerOne.Description = "ServiceOne";
        ServiceInstallerOne.ServiceName = "ServiceOne";
        ServiceInstallerOne.StartType = ServiceStartMode.Automatic;
        ServiceInstallerOne.Parent = ProcessServiceServiceOne;

        System.Collections.Specialized.ListDictionary stateServiceOne = new System.Collections.Specialized.ListDictionary();
        ServiceInstallerObjIntegracao.Install(stateServiceOne);
    }

    private void InstallServiceTwo(string strServiceTwoPath)
    {

        ServiceProcessInstaller ProcessServiceServiceTwo new ServiceProcessInstaller();

        ServiceInstaller ServiceInstallerTwo = new ServiceInstaller();
        InstallContext Context = new System.Configuration.Install.InstallContext();
        String path = String.Format("/assemblypath={0}", strServiceTwoPath);
        String[] cmdline = { path };

        Context = new System.Configuration.Install.InstallContext("", cmdline);
        ServiceInstallerTwo.Context = Context;
        ServiceInstallerTwo.DisplayName = "ServiceTwo";
        ServiceInstallerTwo.Description = "ServiceTwo";
        ServiceInstallerTwo.ServiceName = "ServiceTwo";
        ServiceInstallerTwo.StartType = ServiceStartMode.Automatic;
        ServiceInstallerTwo.Parent = ProcessServiceServiceTwo;

        System.Collections.Specialized.ListDictionary stateServiceTwo = new System.Collections.Specialized.ListDictionary();
        ServiceInstallerObjBRMonitoring.Install(stateServiceTwo);

    }

这是我在stackoverflow社区的第一个问题,英语不是我的母语。 如果我犯了任何错误,我道歉。

1 个答案:

答案 0 :(得分:1)

问题已经解决了!

我只需要从Debug更改为Release并安装Release .exe,现在这两个服务都在运行并且没有任何问题。