Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in WCF services

I am trying to bend my mind around WCF and Dependency Injection is a point that confuses me a bit.

My question is basically: Does WCF support Dependency Injection out of the box, or does one have to rely on some external framework for that?

If there is a way to do this using nothing but WCF, could you give me a link to a simple example?

like image 250
Emil D Avatar asked Jun 09 '11 19:06

Emil D


2 Answers

WCF says nothing about dependency injection as such. Usually you need to use a framework like Unity or StructureMap or Ninject or Castle Windsor to do it.

There are other messages here about how to do it.

like image 24
Peter K. Avatar answered Sep 28 '22 07:09

Peter K.


Check out this blog post by Jimmy Bogard about hooking StructureMap into the WCF integration points that allows him to not have constructor-less service constructors. I know that you're looking for a non-DI Framework, so if you use the approaches provided in the links from Peter K's comment to his answer and combine the roll-you-own DI container with WCF's integration points with IInstanceProvider and IServiceBehavior as described by Jimmy, you should have a DI solution for WCF that doesn't use a third-party DI framework.

It's definitely isn't a 5-minute exercise and will require a bit of code, but hopefully this helps. Let me know if you have questions and I'll update this response. Good luck!

like image 191
David Hoerster Avatar answered Sep 28 '22 05:09

David Hoerster