Being a Good Host 2 – Grouping Multiple Services

Previously I gave a simple overview of hosting a single services in a form. If you’re developing a larger system and you have broken down business across several services, for reasons ranging from code management, to partitioning development tasks across teams, to reducing the compile time for a visual studio solution. You’re likely to want to keep the services separate but kick them off in groups; “Core System Services”, “Supporting Services”, “Reporting/Administration Services”, etc.

The most manual part is to merge the endpoints from all your individual config files into 1 single App.Config file that will accompany your production level deployment approach. This can also be used to package up the initial services mid-way through development when they are unlikely to change and it saves hassle running them as a group.

Watch-out-for: the contract definition in the config file; make sure it’s a fully qualified namespace for each service end point. You add individual references to your host application for each service you’re including.


For this example I’ve simply chosen to run it as a standard windows service, where all I need to do is implement OnStart() and OnStop() and if you think you need to OnPause() and OnContinue() or any of the other ServiceBase methods.

public partial class MultiServiceHost : ServiceBase
   private IList<ServiceHost> hosts = new List<ServiceHost>();

The OnStart() is simply a list of each of your services you’re grouping to run together.

protected override void OnStart(string[] args)
      hosts.Add(new ServiceHost(typeof(PatientService)));
      hosts.Add(new ServiceHost(typeof(MedicalProcedureService)));
      hosts.Add(new ServiceHost(typeof(OtherService)));
      //more hosts...
   //Catch ()...

A loop to kick off all the services, that’s called after they’re all added.

private void StartServices()
   foreach (ServiceHost h in hosts)
   {   h.Open();   }

Closing them all off upon shutdown of the host.

protected override void OnStop()
   foreach (ServiceHost host in _hosts)
      if (host != null)

It’s that simple.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s