Can you host multiple WCF processes in the same windows service?

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 on the same Windows service. Should I worry about ports? I am using mex endpoint

+4
source share
4 answers

Yes, you can. I do this exactly in my project, hosting three separate WCF services in my Windows service. Just make sure every WCF endpoint, i.e. Address / binding / contract tuple, unique.

+3
source

EDIT: SO seems to clip my long code / configuration example, so here is the full explanation: http://thegrenade.blogspot.com/2009/08/hosting-multiple-wcf-services-under.html

Here is an example that might help you:

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 configuration of binding and behavior is distributed between services, but you may need different configurations for different types of services.
  • I use different ports for different services, but you do not need to.

    ...

+8
source

Look at it. Launch WCF ServiceHost with several contracts , this is not what you are asking for, but maybe some use.

Using this plus the InstanceContextMode property of the ServiceBehaviour attribute and the ability to configure "Service Management" , you can get what you want.

+1
source

As with @Matt, I also did this using this link.

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

+1
source

All Articles