Derik Whittaker

Syndication

News


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

[Advertisement]

Comments

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

Awesome.

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): ferventcoder.com/.../WCF-in-IIS-with-Websites-that-have-Multiple-Identities.aspx  (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

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)