How to host 2 WCF services in 1 Windows Service?

前端 未结 4 705
再見小時候
再見小時候 2021-02-04 16:47

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 t

相关标签:
4条回答
  • 2021-02-04 17:17

    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.

    0 讨论(0)
  • 2021-02-04 17:17

    you probably just need 2 service hosts.

    _host1 and _host2.

    0 讨论(0)
  • 2021-02-04 17:34
                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!!!!!!!!!");
    
    0 讨论(0)
  • 2021-02-04 17:36

    Base your service on this MSDN article and create two service hosts. But 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:

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

    0 讨论(0)
提交回复
热议问题