views:

1704

answers:

3

Can the WCF runtime be made to instantiate a service via an IoC container rather than via its usual process? (Also, given a potential clash between the container's lifestyle configuration for the type and the service's InstanceContextBehavior, would this approach be a terrible idea?)

I'm aware that I might be asking the wrong question altogether. My objective is an AOP approach via method interception facilities provided by the container (for example, method enter/exit logging, perf counting, and call throttling, all involving logic and dependencies that I do not want to insert into my service implementation). I imagine WCF provides other ways to approach this, so I would also be curious to hear other recommended approaches.

+2  A: 

Yes, heres implementations using both ObjectBuilder and Spring.NET (and more if you follow the links!):

http://www.infoq.com/news/2008/01/wcf-di

Can't comment on lifestyle and instance context behavior interaction though.

As for more WCF-specific approaches, leveraging WCFs behaviors concept (as the above example does) may also be useful. There are several different types, heres a starting point:

http://mehranikoo.net/CS/archive/2007/02/22/WCFBehaviours.aspx

Cheers,
Matt

Don't know about how good Spring support is, but you don't want to use ObjectBuilder unless you have really good reason to. Other tools are simply much more powerful.
Krzysztof Koźmic
+4  A: 

Short answer - yes it may.

Please take a look at Castle WCF intergration. It let's you use Castle Windsor for WCF, what gives you much more powerful capabilities than just injecting dependencies. It's best to use the trunk version found here. There's not much documentation on it, but take a look at tests. They are easy to follow and will be a good sample code for you. WCF facility let's you do exactly those kinds of thinkgs you're asking about.

Krzysztof Koźmic
Link is broken.
Jonathan Parker
fixed now......
Krzysztof Koźmic
+1  A: 

Hi,

If you fancy the Microsoft Unity container (based on ObjectBuilder2), you can take a look of a blog post I wrote a while ago: http://larswilhelmsen.com/2008/07/02/unity-11-container-behavior-for-wcf/

--larsw

larsw