views:

1768

answers:

3

I have a WCF application that has two Services that I am trying to host in a single Windows Service using net.tcp. I can run either of the services just fine, but as soon as I try to put them both in the Windows Service only the first one loads up. I have determined that the second services ctor is being called but the OnStart never fires. This tells me that WCF is finding something wrong with loading up that second service.

Using net.tcp I know I need to turn on port sharing and start the port sharing service on the server. This all seems to be working properly. I have tried putting the services on different tcp ports and still no success.

My service installer class looks like this:

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

and both services looking very similar

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

you probably just need 2 service hosts.

_host1 and _host2.

Darren Kopp
Yep, I'm not showing the other service. It creates its own host like the one shown.
Craig Tyler
A: 

If you want one Windows service to start two WCF services, you'll need one ServiceInstaller that has two ServiceHost instances, both of which are started in the (single) OnStart method.

You might want to follow the pattern for ServiceInstaller that's in the template code when you choose to create a new Windows Service in Visual Studio - in general this is a good place to start.

Jeremy McGee
+5  A: 

Base your service on this MSDN article and create two service hosts. But instead of acctually calling each service host directly you can break it out to as many classes you want which defines each service you want to run:

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

In the body of the windows service host, call the different classes:

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


    }

Then you can add as many WCF services as you like to one windows service host.

REMEBER to call the stop methods as well....

superwiren