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

前端 未结 4 1014
耶瑟儿~
耶瑟儿~ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 serviceHosts = new List();
    
        public void OpenAll() {
            OpenHost();
            OpenHost();
            ...
        }
    
        public void CloseAll() {
            foreach (ServiceHost serviceHost in serviceHosts)
                serviceHost.Close();
        }
    
        private void OpenHost() {
            Type type = typeof(T);
            ServiceHost serviceHost = new ServiceHost(type);
            serviceHost.Open();
            serviceHosts.Add(serviceHost);
        }
    }
    
    /// 
    /// Enables application to be installed as a Windows Service by running InstallUtil
    /// 
    [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.

      ...

提交回复
热议问题