It is well known that WCF service can be hosted as a self-hosted application, IIS web service or a Windows service. This post is dedicated to the Windows service hosting and it goes through some advanced techniques on how to host many WCF services in one windows service.
So, here I have got three projects: multiple WCF services project, a client and a Windows service installer. Let us start with the WCF services. This project has three service interfaces and correspondently three service implementations.

Sample interface looks like this:
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoAction1();
}
And its implementation is very simple
public class Service1 : WindowsManagedService, IService1
{
public Service1() : base(typeof(Service1)) { }
public string DoAction1()
{
return "Action 1";
}
}
These are the interfaces in WCF service project

Class diagram for the services that implement those interfaces

You must have noticed that there are a few other types like IWindowsManagedService and WindowsManagedService - this is basically a bridge that allows WCF services to be hosted within Windows service.
public interface IWindowsManagedService
{
void Start();
void Stop();
}
public abstract class WindowsManagedService : IWindowsManagedService
{
private ServiceHost serviceHost;
protected WindowsManagedService(Type serviceType)
{
serviceHost = new ServiceHost(serviceType);
}
public void Start()
{
serviceHost.Open();
}
public void Stop()
{
serviceHost.Close();
}
}
Next, let’s take a quick look into the host project. This project contains a configuration file for the WCF services. It also has one installer class which is picked up by installutil. But the main type here is WindowsManagedServices which uses reflection to find WCF services that can be hosted in one Windows service
public class WindowsManagedServices : ServiceBase
{
private List<IWindowsManagedService> services;
public WindowsManagedServices()
{
services = new List<IWindowsManagedService>();
var assembly = Assembly.GetAssembly(typeof(IWindowsManagedService));
var types = assembly.GetTypes();
foreach (var type in types)
{
if (!type.IsAbstract && typeof(IWindowsManagedService).IsAssignableFrom(type))
{
var serviceObject = (IWindowsManagedService)Activator.CreateInstance(type);
services.Add(serviceObject);
}
}
}
protected override void OnStart(string[] args)
{
foreach (var service in services)
{
service.Start();
}
}
protected override void OnStop()
{
foreach (var service in services)
{
service.Stop();
}
}
}
Then, I used installutil to install the windows service and here is what I end up with
- 1 Windows Service WCFServices
- 3 WCF services
http://localhost:8083/WCFServices/IService1
http://localhost:8083/WCFServices/IService2
http://localhost:8083/WCFServices/IService3
Lastly, a sample client to this application
class Program
{
static void Main(string[] args)
{
IService1 client1 = new Service1Client();
IService2 client2 = new Service2Client();
IService3 client3 = new Service3Client();
Console.WriteLine("Client 1 says: " + client1.DoAction1());
Console.WriteLine("Client 2 says: " + client2.DoAction2());
Console.WriteLine("Client 3 says: " + client3.DoAction3());
Console.ReadLine();
}
}

The whole project can be downloaded using the link below. Unzip it, run the solution file and hit F5 in Visual Studio 2010. It should start two projects: services and the client.
WCFServiceHosting.zip (46.48 kb) [Downloads: 494] [UPDATED after discovery of the client's port mismatch)

If you enjoyed this post, make sure you subscribe to my RSS feed!
3f9c2985-f063-447f-84ab-8261ee842cac|0|.0