WCF service instantiation via IoC container


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.

Best Solution

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.

Related Question