Scott Watermasysk

Still Learning to Code

IofC on Azure

One of the first things everyone must learn about hosting an application on Azure is the restricted context in which it runs. Microsoft has been a bit vague on what exactly this means, but roughly it is a more locked down version of Medium Trust. For obvious reasons, there is no access to registry, limited file system access, in the case of this post, more strict policy on code access.

There are quite a few components for .NET developers choose from when it comes to implementing Inversion of Control (IofC). They all vary in a variety of ways and no one is the true answer. I am not an expert on IofC components, but for most of the common tasks, anyone of the available components should work for you. It is just a matter of finding one you (and your team) are comfortable with. However, in the case of using IofC while hosting an application on Azure, you may need to do a bit more home work.

While I have not tested all the components available, I did manage to get two of them to work and as far as I can tell, the core areas I am using in my pet Azure project seem to function perfectly.

1. Ninject – to use Ninject on Azure, you need to set one option when creating your container: UseReflectionBasedInjection = true. So my Initialize method looks something like this:

public static void Initialize(params StandardModule[] modules)
    var options = new KernelOptions() {UseReflectionBasedInjection = true};
    kernal = new StandardKernel(options, modules);

Note: For Ninject I am using the latest bits from the trunk, but Nate let me that the 1.0 release should work just fine as well.

2. StructureMap – through a bit of trial and error, I was able to get StructureMap to work on Azure with no configuration changes after I removed a couple attributes which appeared to no longer be in use. I sent email to the StructureMap Google Group with the changes I made and a couple days ago Jeremy Miller released version 2.5.3 which contains the changes.

If you have managed to get any other containers to work on Azure, please leave a comment below and I will update this post.