12

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?

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
Emil D
  • 1,864
  • 4
  • 23
  • 40
  • 1
    possible duplicate of [How do I pass values to the constructor on my wcf service?](http://stackoverflow.com/questions/2454850/how-do-i-pass-values-to-the-constructor-on-my-wcf-service) – Mark Seemann Jun 09 '11 at 21:23

2 Answers2

10

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!

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
2

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.

Community
  • 1
  • 1
Peter K.
  • 8,028
  • 4
  • 48
  • 73
  • 1
    You don't need a DI framework to do dependency injection. They can come in handy though ;-) – Steven Jun 09 '11 at 19:43
  • :-) True. I was reading through [this](http://kenegozi.com/blog/2008/01/17/its-my-turn-to-build-an-ioc-container-in-15-minutes-and-33-lines) and [this](http://ayende.com/blog/2886/building-an-ioc-container-in-15-lines-of-code). How hard can it be?!? :-) – Peter K. Jun 09 '11 at 19:45
  • 1
    After building one for a year, I can conclude that it is actually very easy ;-) – Steven Jun 09 '11 at 19:58
  • 1
    So, does WCF offer any control over the instantiation of the service contract implementation class, other than specifying the name of the implementation class? What if said class doesn't have a default constructor? – Emil D Jun 09 '11 at 20:17