3

Possible Duplicate:
Unity Dependency Injection for WCF services
Using Unity Dependency Injection with WCF services

I am new to Unity IoC Container.. I am working on a WCF application (hosted on IIS). I want to use Unity to inject objects like logger, database etc...

As far as I know, the container needs to registered at the application start-up... Where can I do that in WCF?

Once setup is done, how can I refer it across the WCF application? I mean do I have make it static or something like that?

Community
  • 1
  • 1
Ali
  • 309
  • 1
  • 5
  • 20
  • 2
    See also http://stackoverflow.com/questions/2042609/injecting-data-to-a-wcf-service – TrueWill Jan 24 '12 at 03:25
  • @TrueWill first one has an answer with a dead link. – gideon Jan 24 '12 at 03:27
  • See if this helps :http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx from this post : http://stackoverflow.com/questions/7355501/using-unity-dependency-injection-with-wcf-services – gideon Jan 24 '12 at 03:32
  • @gideon I fixed the dead link. – TrueWill Jan 24 '12 at 03:40
  • @TrueWill the first link looks like what I want, I am going to give it a shot. Thanks – Ali Jan 24 '12 at 03:45

1 Answers1

1

WCF, along with any other http- or request-based application, is distinctly different than a standard UI application. Initialization of your IOC container won't occur until the first request is received by your application, so you have to go about things a little differently.

For a standard WCF application hosted in IIS, the easiest way to do this would be through an HTTP module that initializes your Unity container the first time a request is received. If you're going to do things properly, however, you should investigate a custom InstanceProvider class that will allow Unity to provide the instance of the WCF service implementation, rather than having a singelton WCF class using Unity to retrieve its dependent components.

I don't know Unity very well, myself -- I prever Ninject and StructureMap -- but some quick googling resulted in this link: http://initializecomponent.blogspot.com/2008/06/integrating-unity-with-wcf.html. Looks like someone's already done most of the heavy lifting for you on this. :)

Kintar
  • 71
  • 2