Can you host multiple WCF processes in a single windows service?

前端 未结 4 1001
耶瑟儿~
耶瑟儿~ 2020-12-30 16:32

I have a WCF process hosted in a windows service. I am wondering if I can safely have multiple WCF processes that do different things hosted in the same windows service. Do

相关标签:
4条回答
  • 2020-12-30 17:01

    Yes, you can. I am doing this exact thing in my project, hosting three separate WCF services inside my Windows service. Just make sure that each WCF endpoint, i.e., the address/binding/contract tuple, is unique.

    0 讨论(0)
  • 2020-12-30 17:05

    Have a look at this Run WCF ServiceHost with multiple contracts its not exactly what you are asking for but maybe of some use.

    Using that plus the InstanceContextMode property of the ServiceBehaviour attribute and the ability to configure Service throttling you should be able to get what you want.

    0 讨论(0)
  • 2020-12-30 17:16

    As with @Matt, I've done it too with help from this link.

    http://www.codeproject.com/KB/WCF/generic_wcf_host.aspx

    0 讨论(0)
  • 2020-12-30 17:18

    EDIT: SO seems to be trimming my lengthy code/config example so there's a complete explanation here: http://thegrenade.blogspot.com/2009/08/hosting-multiple-wcf-services-under.html

    Here's an example that may help get you going:

    class Program {
        static void Main() {
            if (Environment.UserInteractive) {
                ServiceManager serviceManager = new ServiceManager();
                serviceManager.OpenAll();
                Console.ReadKey();
                serviceManager.CloseAll();
            }
            else
                ServiceBase.Run(new WindowsService());
        }
    }
    
    public class WindowsService : ServiceBase
    {
        public static string WindowsServiceName = "Windows Service Name";
        public static string WindowsServiceDescription = "Windows Service Description";
        public static string WindowsServiceUsername = @".\username";
        public static string WindowsServicePassword = "password";
    
        private readonly ServiceManager serviceManager = new ServiceManager();
    
        private readonly IContainer components = new Container();
    
        protected override void Dispose(bool disposing) {
            if (serviceManager != null) serviceManager.CloseAll();
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }
    
        public WindowsService() {
            ServiceName = WindowsServiceName;
            CanStop = true;
        }
    
        protected override void OnStart(string[] args) {
            base.OnStart(args);
            serviceManager.OpenAll();
        }
    
        protected override void OnStop() {
            serviceManager.CloseAll();
            base.OnStop();
        }
    }
    
    public class ServiceManager {
        readonly List<ServiceHost> serviceHosts = new List<ServiceHost>();
    
        public void OpenAll() {
            OpenHost<Service1>();
            OpenHost<Service2>();
            ...
        }
    
        public void CloseAll() {
            foreach (ServiceHost serviceHost in serviceHosts)
                serviceHost.Close();
        }
    
        private void OpenHost<T>() {
            Type type = typeof(T);
            ServiceHost serviceHost = new ServiceHost(type);
            serviceHost.Open();
            serviceHosts.Add(serviceHost);
        }
    }
    
    /// <remarks>
    /// Enables application to be installed as a Windows Service by running InstallUtil
    /// </remarks>
    [RunInstaller(true)]
    public class WcfServiceHostInstaller : Installer {
        public WcfServiceHostInstaller() {
            Installers.Add(new ServiceInstaller
                               {
                                   StartType = ServiceStartMode.Automatic,
                                   ServiceName = WindowsService.WindowsServiceName,
                                   Description = WindowsService.WindowsServiceDescription
                               });
            Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.User, Username = WindowsService.WindowsServiceUsername, Password = WindowsService.WindowsServicePassword });
        }
    }
    

    And some configuration

    • Here, the binding & behaviour configuration is shared across services but you may need different configurations for different types of services.
    • I use different ports for different services, but you don't have to.

      ...

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