如何在1个Windows服务中托管2个WCF服务?

问题描述:

我有一个WCF应用程序,它有两个服务,我尝试使用net.tcp在单个Windows服务中托管。我可以运行其中任何一项服务,但只要我尝试将它们都放入Windows服务中,只有第一个加载。我已经确定第二个服务ctor正在被调用,但OnStart从不会触发。这告诉我WCF在加载第二个服务时发现有问题。如何在1个Windows服务中托管2个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(); 
     } 
} 

将您的服务基于此MSDN article并创建两个服务主机。 但是,而不是实际直接调用每个服务的主机,你可以打破它为很多类,只要你想,它定义要运行的每个服务:

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(); 


    } 

然后当你喜欢一个Windows服务的主机,你可以添加任意多的WCF服务。

REMEBER调用停止方法以及....

+0

这个工程!与ServiceBase.Run(ServiceBase [])不同,因为Microsoft模板意味着可以工作......谢谢,你刚刚救了我的一天! – DaveN59 2011-09-23 18:48:42

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

_host1和_host2。

+0

是的,我不显示其他服务。它创建自己的主机,如图所示。 – 2008-09-10 17:01:55

如果您希望一个Windows服务启动两个WCF服务,则需要一个ServiceInstaller,它具有两个ServiceHost实例,它们都是在(单个)OnStart方法中启动的。

当您选择在Visual Studio中创建新的Windows服务时,您可能想要遵循模板代码中的ServiceInstaller模式 - 通常这是一个很好的开始。

  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!!!!!!!!!");