Multiple WCF Services on a single port

One of the more common problems most WCF developers have is to make multiple service hosted on windows service (or console service for that matter) and the common misconception is that Service Host can only listen to one port. Well that may partly be true but you can definitely take advantage of addressing mechanism in order to overcome this.

e.g You have two service PersonService where service contract is IPersonService and another one as AccountingService whose service contract is IAccountingService.

You can host both of them on a windows service host but can share single port if you re-arrange the addressing scheme in this way http://localhost:6962/IAccountingService/ and http://localhost:6962/IPersonService/ .You may be wondering what is new in this since all of us have been doing this in IIS since the beginning. The difference between IIS is that when you use .svc files, it kinds of forces you to choose multiple host files so above addressing becomes like http://localhost:6962/AccountingService.svc and http://localhost:6962/PersonService.svc.

To make it easy to host multiple service share single port without separate host files, I have created this helper class called MultipleServiceHost however it does not inherit from ServiceHost. You can use the same mechanism in either console application or windows service as it support starting, stopping and restarting methods.

StartServices method describes the behavior, it is basically appending the contract name of the WCF service in the address so that multiple service can share the same port.

1 public void StartServices() 2 { 3 _serviceHosts.Clear(); 4 5 foreach (var type in _mappings) 6 { 7 Type contractType = type.Key; 8 Type implementationType = type.Value; 9 10 var serviceHost = new ServiceHost(implementationType); 11 var endpoint = serviceHost.AddServiceEndpoint(contractType, 12 _binding,_baseUrl + "/" + contractType.Name); 13 14 var metadataBehavior = new ServiceMetadataBehavior(); 15 16 serviceHost.Description.Behaviors.Add(metadataBehavior); 17 serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), 18 MetadataExchangeBindings.CreateMexHttpBinding(), 19 _baseUrl + "/" + contractType.Name + "/MEX"); 20 21 var serviceDebugBehaviour = 22 serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>(); 23 serviceDebugBehaviour.IncludeExceptionDetailInFaults = true; 24 25 Console.WriteLine("Service is running at"+ endpoint.Address); 26 27 serviceHost.Open(); 28 serviceHost.Faulted += new EventHandler(ServiceHostFaulted); 29 _serviceHosts.Add(serviceHost); 30 } 31 32 }

Constructor code

1 public class MultipleServiceHost 2 { 3 private readonly Dictionary<Type,Type> _mappings= 4 new Dictionary<Type, Type>(); 5 private readonly Binding _binding; 6 private readonly string _baseUrl; 7 8 public MultipleServiceHost(Dictionary<Type, Type> mappings, 9 string baseUrl, Binding binding) 10 { 11 _mappings = mappings; 12 _baseUrl = baseUrl; 13 _binding = binding; 14 } 15 16 private readonly List<ServiceHost> _serviceHosts = 17 new List<ServiceHost>();

Caller Code:

1 static void Main(string[] args) 2 { 3 var mappings = new Dictionary<Type, Type> 4 { 5 {typeof (IAccountingService), typeof (AccountingService)}, 6 {typeof(IPersonService), typeof(PersonService)} 7 }; 8 9 var multipleServiceHost = new MultipleServiceHost(mappings, "http://localhost:6962", GetDefaultBinding()); 10 multipleServiceHost.StartServices(); 11 12 Console.WriteLine("Services are running,press any key to stop services"); 13 Console.ReadKey(); 14 multipleServiceHost.StopServices(); 15 16 //multipleServiceHost.RestartServices(); 17 //Console.WriteLine("Services are running,press any key to stop services"); 18 //Console.ReadKey(); 19 //multipleServiceHost.StopServices(); 20 21 } 22 23 public static Binding GetDefaultBinding() 24 { 25 return new BasicHttpBinding(); 26 } 27

Complete Sample can be found here

Advertisements

One thought on “Multiple WCF Services on a single port

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s