After having to write new instance providers, host factories and service behaviors for almost every new WCF project; I decided to write a simple reusable component for WCF and dependency injection and put it on codeplex so that I never had to write that again.
The idea is simple, when creating service hosts you more often then not want a centralized controlling factory that handles your dependency wiring and life time management of said dependencies. WCF requires you to add custom code to the initialization extension points.
Enter the Common Service Host. Based on the Common Service Locator interface and model it allows for a single library with classes for any DI-Framework to automatically wire up your service instances. From the codeplex documentation:
Example host configuration:
public class AppContainer : UnityContainerProvider { public void EnsureConfiguration() { UnityContainer.RegisterType(); } }
Example self-hosted usage:
using(var host = new CommonServiceHost()) { host.Open(); }
Example usage for a .svc file:
<%@ 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" %>
Included providers in the current release for Unity and spring.net
Get your copy here: http://commonservicehost.codeplex.com