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:
  • Coordination of shared dependencies and their life time with a well thought through dependency framework. E.g, built in session / connection-management for PerCall / PerSession WCF instance modes in StructureMap / Unity
  • Simplify unit-testing of the Service class with the ability to Mock all dependencies.
  • Easily change or extend dependencies without having to touch the services that rely on them. E.g change a dependency on a repository to use the cached version instead of the DB-Only one, in one single place without the need to touch every service.

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:
  • Unity
  • Spring.NET


This project is sponsored by:

Sogeti

Last edited May 31, 2010 at 7:28 AM by lowendahl, version 14