<p>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.</p>

<p>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.</p>

<p>My service installer class looks like this:</p>

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

<p>and both services looking very similar</p>

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

[{'answer_id': 54424, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>you probably just need 2 service hosts.</p>\n\n<p>_host1 and _host2.</p>\n'}, {'answer_id': 77401, 'author': 'Jeremy McGee', 'author_id': 3546, 'author_profile': 'https://Stackoverflow.com/users/3546', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 90870, 'author': 'Wiren', 'author_id': 2538222, 'author_profile': 'https://Stackoverflow.com/users/2538222', 'pm_score': 5, 'selected': True, 'text': '<p>Base your service on this <a href="http://msdn.microsoft.com/en-us/library/ms733069.aspx" rel="nofollow noreferrer">MSDN article</a> and create two service hosts. \nBut instead of actually calling each service host directly, you can break it out to as many classes as you want which defines each service you want to run:</p>\n\n<pre><code>internal class MyWCFService1\n{\n    internal static System.ServiceModel.ServiceHost serviceHost = null;\n\n    internal static void StartService()\n    {\n        if (serviceHost != null)\n        {\n            serviceHost.Close();\n        }\n\n        // Instantiate new ServiceHost.\n        serviceHost = new System.ServiceModel.ServiceHost(typeof(MyService1));\n        // Open myServiceHost.\n        serviceHost.Open();\n    }\n\n    internal static void StopService()\n    {\n        if (serviceHost != null)\n        {\n            serviceHost.Close();\n            serviceHost = null;\n        }\n    }\n};\n</code></pre>\n\n<p>In the body of the windows service host, call the different classes:</p>\n\n<pre><code>    // Start the Windows service.\n    protected override void OnStart( string[] args )\n    {\n        // Call all the set up WCF services...\n        MyWCFService1.StartService();\n        //MyWCFService2.StartService();\n        //MyWCFService3.StartService();\n\n\n    }\n</code></pre>\n\n<p>Then you can add as many WCF services as you like to one windows service host.</p>\n\n<p>REMEBER to call the stop methods as well....</p>\n'}, {'answer_id': 9159152, 'author': 'Eswararao', 'author_id': 1192102, 'author_profile': 'https://Stackoverflow.com/users/1192102', 'pm_score': 2, 'selected': False, 'text': '<pre><code>            Type serviceAServiceType = typeof(AfwConfigure);\n            Type serviceAContractType = typeof(IAfwConfigure);\n\n            Type serviceBServiceType = typeof(ConfigurationConsole);\n            Type serviceBContractType = typeof(IConfigurationConsole);\n\n            Type serviceCServiceType = typeof(ConfigurationAgent);\n            Type serviceCContractType = typeof(IConfigurationAgent);\n\n            ServiceHost serviceAHost = new ServiceHost(serviceAServiceType);\n            ServiceHost serviceBHost = new ServiceHost(serviceBServiceType);\n            ServiceHost serviceCHost = new ServiceHost(serviceCServiceType);\n            Debug.WriteLine("Enter1");\n            serviceAHost.Open();\n            Debug.WriteLine("Enter2");\n            serviceBHost.Open();\n            Debug.WriteLine("Enter3");\n            serviceCHost.Open();\n            Debug.WriteLine("Opened!!!!!!!!!");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54419', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5408/']