Quick fact

Common Service Host is a generic Windows Communication Foundation Service Host and factory that uses the Common Service Locator to enable service object creation with any Dependency Inversion framework, like Unity, Spring.Net or StructureMap. This enables WCF services to utilize the strength of Inversion of Control and Life Time Management by a centralized factory.

Why use a Common Service Host for WCF?

There are several advantages in allowing a DI-Framework hook into the creation of service objects. The common denominator being that it will be easier to follow sound Software Craftmanship principles when setting up and maintaining your services. Advantages include:

Since the Common Service Host (CSH) only have a dependency on the Common Service Locator it allows you as a developer to choose the DI-Framework you like to handle the service resolution. CSH comes with providers for the most popular frameworks but is extreamly easy to extend with others.

Is it hard to use?

No, the emphasis on the CSH has been simplicity. To utilize DI with your WCF-Services you create a subclass for the proper provider and implement the abstract member EnsureConfiguration. You then supply your configured class to the host as a generic parameter.

Example container configuration:
public class AppContainer : UnityContainerProvider
{
        public void EnsureConfiguration()
        {
            UnityContainer.RegisterType<IRepository, Repository>();
        }
} 

Example host creation:
using(var host = new CommonServiceHost<AppContainer>())
{
    host.Open();
} 

Example .svc service Factory usage:
<%@ ServiceHost Language="C#" 
      Service="Sogeti.Guidelines.Examples.Service1" 
      Factory="Sogeti.Guidelines.WCF.Hosting.CommonServiceHostFactory`1[[Sogeti.Guidelines.Examples.AppContainer,
 Sogeti.Guidelines.Examples.Service]], Sogeti.Guidelines.WCF.Hosting" %> %> 

Best practices would be to create a placeholder factory class:
public class AppServiceFactory : CommonServiceHostFactory<AppContainer> {} 

Which makes it possible to simplify the configuration:
 <%@ ServiceHost Language="C#" 
      Service="Sogeti.Guidelines.Examples.Service1" 
      Factory="Sogeti.Guidelines.Examples.AppServiceFactory" %> %> 

There are complete examples in the source download.

Included providers

The following Framework providers are already included in the 0.1 release:

This project is sponsored by:

Sogeti