Search code examples
dependency-injectioninversion-of-controlazure-web-roles

Implementing dependency injection with Azure WebRole in full trust using a single container


I'm trying to implement dependency injection for a WCF service hosted in an Azure WebRole that runs in full-trust mode (So there are two separate processes).

I've tried Ninject so far and I had no problems injecting dependencies into the WCF service using the method described in this article: http://tinyurl.com/cnxrscg

However, I still have two unresolved issues:

  1. I can't seem to find a way to instantiate the Kernel before RoleEntryPoint, so that I can inject the dependencies I need into it. Am I supposed to create it from there or is there a better practice?
  2. Since running in full-trust means two processes, does this also mean I have no other choice but to create two separate Kernel instances?

Note: I'm not interested in a Ninject-specific solution. As a matter of fact, I've stumbled across many shortcomings while dealing with it so I'm considering a different IoC container. Which one works best with Azure?

Thanks!


Solution

  • Each AppDomain needs its own container. Sharing a container over AppDomains or even across processes is something that is typically not supported by a container, and if it is, it costs a lot of performance, since all the marshalling that will be going on between the two AppDomains. Besides, each AppDomain typically represents its own application, and each application often needs its unique registration. This by itself justifies giving each AppDomain its own container.