如何在一个Windows服务中托管两个WCF服务?

8
我有一个WCF应用程序,其中包含两个服务,我试图使用net.tcp在单个Windows服务中托管它们。我可以正常运行任何一个服务,但是一旦我尝试将它们都放入Windows服务中,只有第一个服务会加载。我已经确定第二个服务的构造函数被调用了,但OnStart从未触发。这告诉我WCF在加载第二个服务时出现了问题。
我知道使用net.tcp需要打开端口共享并在服务器上启动端口共享服务。这似乎都正常工作。我尝试将服务放在不同的tcp端口上,但仍然没有成功。
我的服务安装程序类如下:
 [RunInstaller(true)]
 public class ProjectInstaller : Installer
 {
      private ServiceProcessInstaller _process;
      private ServiceInstaller        _serviceAdmin;
      private ServiceInstaller        _servicePrint;

      public ProjectInstaller()
      {
            _process = new ServiceProcessInstaller();
            _process.Account = ServiceAccount.LocalSystem;

            _servicePrint = new ServiceInstaller();
            _servicePrint.ServiceName = "PrintingService";
            _servicePrint.StartType = ServiceStartMode.Automatic;

            _serviceAdmin = new ServiceInstaller();
            _serviceAdmin.ServiceName = "PrintingAdminService";
            _serviceAdmin.StartType = ServiceStartMode.Automatic;

            Installers.AddRange(new Installer[] { _process, _servicePrint, _serviceAdmin });
      }   
}

两个服务看起来非常相似

 class PrintService : ServiceBase
 {

      public ServiceHost _host = null;

      public PrintService()
      {
            ServiceName = "PCTSPrintingService";
            CanStop = true;
            AutoLog = true;

      }

      protected override void OnStart(string[] args)
      {
            if (_host != null) _host.Close();

            _host = new ServiceHost(typeof(Printing.ServiceImplementation.PrintingService));
            _host.Faulted += host_Faulted;

            _host.Open();
      }
}
4个回答

11
基于这篇MSDN文章来创建两个服务主机。但是,您不需要直接调用每个服务主机,而是可以将其拆分为任意数量的类,每个类定义您要运行的每个服务:
internal class MyWCFService1
{
    internal static System.ServiceModel.ServiceHost serviceHost = null;

    internal static void StartService()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        // Instantiate new ServiceHost.
        serviceHost = new System.ServiceModel.ServiceHost(typeof(MyService1));
        // Open myServiceHost.
        serviceHost.Open();
    }

    internal static void StopService()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
};

在Windows服务主机的主体中,调用不同的类:
    // Start the Windows service.
    protected override void OnStart( string[] args )
    {
        // Call all the set up WCF services...
        MyWCFService1.StartService();
        //MyWCFService2.StartService();
        //MyWCFService3.StartService();


    }

然后您可以将尽可能多的WCF服务添加到一个Windows服务主机中。

请记得同时调用停止方法....


这个可行!与Microsoft模板所暗示的ServiceBase.Run(ServiceBase[])完全不同...谢谢,你刚刚救了我的一天! - DaveN59

2
            Type serviceAServiceType = typeof(AfwConfigure);
            Type serviceAContractType = typeof(IAfwConfigure);

            Type serviceBServiceType = typeof(ConfigurationConsole);
            Type serviceBContractType = typeof(IConfigurationConsole);

            Type serviceCServiceType = typeof(ConfigurationAgent);
            Type serviceCContractType = typeof(IConfigurationAgent);

            ServiceHost serviceAHost = new ServiceHost(serviceAServiceType);
            ServiceHost serviceBHost = new ServiceHost(serviceBServiceType);
            ServiceHost serviceCHost = new ServiceHost(serviceCServiceType);
            Debug.WriteLine("Enter1");
            serviceAHost.Open();
            Debug.WriteLine("Enter2");
            serviceBHost.Open();
            Debug.WriteLine("Enter3");
            serviceCHost.Open();
            Debug.WriteLine("Opened!!!!!!!!!");

1

如果你想要一个Windows服务启动两个WCF服务,你需要一个ServiceInstaller,并在其中创建两个ServiceHost实例,在单个OnStart方法中同时启动它们。

你可能想要遵循Visual Studio中创建新的Windows服务时模板代码中的ServiceInstaller模式-一般来说这是一个很好的起点。


0

你可能只需要两个服务主机。

_host1 和 _host2。


是的,我没有展示另一个服务。它会像所示一样创建自己的主机。 - Craig Tyler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接