Derik Whittaker



Custom Service Host for WCF

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" 
    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 )

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
Filed under: , ,



Krzysztof Kozmic wrote re: Custom Service Host for WCF
on 11-24-2009 3:09 AM

Or, if you use Castle Windsor, you can use the fantastic WCF Facility which not only injects your service with all the good stuff, but also adds some neat features to it.

Note to self... I've gotta blog about it more.

Rob Reynolds wrote re: Custom Service Host for WCF
on 11-24-2009 11:27 AM


We've done something similar to this in the past due to the limitations on WCF with IIS and having multiple identities with a website (also known as the multiple scheme issue):  (warning - it's in VB, it was 2007). :D

One of the commenters points to a better way to handle the IIS issue with BaseAddressPrefixFilters with .NET 3.5.

We've moved away from WCF, but if I ever find myself moving back to it, I will definitely look at the Castle facility.

Scott Banwart's Blog wrote Distributed Weekly 26
on 02-26-2010 9:40 AM

BizTalk/AppFabric My recap of the PDC 2009 preview of the BizTalk V.Next prototype BizTalk Server Strong Roadmap and Innovations Preview presented at PDC AppFabric and BizTalk REST QCon REST Tutorial Slides Deriving FOREST SOA SOA and Cloud QCon San Fr

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)