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

标签 c# .net wpf windows service

我有以下问题。

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

Error 1053: The service did not respond to the start or control request in a timely fashion

这是我启动它们的代码:

//*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 社区的第一个问题,英语不是我的母语。 如果我犯了任何错误,我深表歉意。

最佳答案

问题已解决!

我只需要从 Debug 更改为 Release 并安装 Release .exe,现在这两个服务都可以正常运行。

关于c# - 在同一应用程序中启动两个 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38997088/

相关文章:

c# - 名为响应的脚本引用不再有效

c# - WPF 用户控制资源

c# - XAML 中的组合框选择更改

.net - 使用 vb.net 从 Sql 数据库插入、更新和删除数据?

c# - 如何将应用程序转换为在中等信任级别下运行?

c# - 用Kinect Skeleton关节模拟多点触控(SDK1.5)

c# - 如何将 Azure DocumentDB 文档类转换为我的 POCO 类?

c# - 在Unity3D C#中获取精确的旋转角度

c# - ASP.NET MVC 3 : Output specific view for concrete implementation

c# - 如何在C#中继承C++/CLI接口(interface)?