In this great post by Jimmy Bogard or this Dimecasts on the subject we take a look at how to add IoC (Inversion of Control) support to your WCF application in order to support DI (Dependency Inversion). However, the one thing both the post and Dimecasts fail to take into account is what if you are not hosting your WCF service in IIS?
In Jimmy’s post he instructs you to override the default factory in the code behind for your service so you can specify the ServiceHost.
<%@ ServiceHost Language="C#" Debug="true"
Service="SMExample.Wcf.CustomerSearchService"
Factory="SMExample.Wcf.StructureMapServiceHostFactory" %>
However, in my case I am not hosting my WCF service in IIS, but rather in a windows service. Because I do not have a code behind file where do I set my custom Factory? Simple, In place of registering your service with the default build in Service Host class, you register it with your custom one (see Jimmy’s post for more details on this).
if ( _wcfServiceHost == null )
{
_wcfServiceHost = new StructureMapServiceHost( typeof( QueuingService ) );
}
if( _wcfServiceHost.State != CommunicationState.Opened )
{
_wcfServiceHost.Open();
}
As you can see, even if you are hosting your WCF service outside of IIS, setting up your custom Service Host for IoC is cake.
Till next time,
Posted
11-23-2009 6:10 AM
by
Derik Whittaker