2008-09-10 13 views
8

Tengo una aplicación WCF que tiene dos servicios que intento alojar en un solo servicio de Windows usando net.tcp. Puedo ejecutar cualquiera de los servicios muy bien, pero tan pronto como trato de ponerlos a ambos en el Servicio de Windows, solo el primero se carga. He determinado que se llama al segundo administrador de servicios, pero el OnStart nunca se activa. Esto me dice que WCF está encontrando algo mal al cargar ese segundo servicio.¿Cómo alojar 2 servicios WCF en 1 servicio de Windows?

Uso de net.tcp Sé que necesito activar el uso compartido de puertos e iniciar el servicio de intercambio de puertos en el servidor. Todo esto parece estar funcionando correctamente. Intenté poner los servicios en diferentes puertos tcp y todavía no tuve éxito.

Mi clase de instalación de servicio es el siguiente:

[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 }); 
     } 
} 

y servicio de un aspecto muy parecido

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

Respuesta

11

Base su servicio en este MSDN article y cree dos servidores de servicio. Pero en lugar de realmente llamando a cada host de servicio directamente, se puede romper a tantas clases como quiera que define cada servicio que desea ejecutar:

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; 
     } 
    } 
}; 

En el cuerpo del huésped servicio de Windows, llame al diferentes clases:

// Start the Windows service. 
    protected override void OnStart(string[] args) 
    { 
     // Call all the set up WCF services... 
     MyWCFService1.StartService(); 
     //MyWCFService2.StartService(); 
     //MyWCFService3.StartService(); 


    } 

Luego puede agregar tantos servicios WCF como desee a un host de servicio de Windows.

RECUERDE a llamar a los métodos de parada, así ....

+0

Esto funciona! Muy diferente a ServiceBase.Run (ServiceBase []), como implica la plantilla de Microsoft, funcionaría ... ¡Gracias, acabas de salvarme el día! – DaveN59

0

es probable que sólo necesita 2 hosts de servicios.

_host1 y _host2.

+0

Sí, no estoy mostrando el otro servicio. Crea su propio host como el que se muestra. –

1

Si desea que un servicio de Windows inicie dos servicios de WCF, necesitará un instalador de servicio que tenga dos instancias de ServiceHost, las cuales se inician en el método (único) OnStart.

Es posible que desee seguir el patrón de ServiceInstaller que se encuentra en el código de la plantilla cuando elige crear un nuevo servicio de Windows en Visual Studio; en general, este es un buen lugar para comenzar.

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