Pregunta

Tengo una aplicación WCF que tiene dos servicios que intento alojar en un único servicio de Windows usando net.tcp.Puedo ejecutar cualquiera de los servicios sin problemas, pero tan pronto como intento poner ambos en el servicio de Windows, solo se carga el primero.He determinado que se llama al segundo ctor de servicios pero OnStart nunca se activa.Esto me dice que WCF está encontrando algún problema al cargar ese segundo servicio.

Usando net.tcp, sé que necesito activar el uso compartido de puertos e iniciar el servicio de uso compartido 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 instalador de servicios se ve así:

 [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 ambos servicios se ven muy similares

 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();
      }
}
¿Fue útil?

Solución

Base su servicio en esto artículo de MSDN y cree dos hosts de servicio.Pero en lugar de llamar directamente a cada host de servicio, puede dividirlo en tantas clases como desee, lo 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 host de servicios de Windows, llame a las 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 servicios de Windows.

RECUERDE llamar también a los métodos de parada....

Otros consejos

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

Si desea que un servicio de Windows inicie dos servicios WCF, necesitará un ServiceInstaller 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.

probablemente solo necesites 2 hosts de servicio.

_host1 y _host2.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top