Question

My company has got a deployment policy (I skip the details) such that any 3rd party software should be installed in the GAC, whilst our libraries are in Web/bin folder. But this approach doesn't work with Ninject and MVC 3/4. Let's follow an example:

This is my dependencies binding code:

public class RequestorDependenciesRegistration : NinjectModule
{
  public override void Load()
  {
    Bind<IMyDearDependency>().To<MyDearImplementation>();
  }
}

And this is my MVC controller:

public MyController(IMyDearDependency something) {
  this.something = something; // 'something' is set only if Ninject dlls are in Web/bin... X-(
}

If Ninject dlls are in the GAC, it loads the module correctly, but when instantiating the MVC Controller the dependency is not injected (in some cases is null, in some cases MVC returns an error "No parameterless constructor etc etc"). If I manually copy Ninject*.dll in the Web/bin folder, than everything works fine, even without restarting IIS! Can't really understand why...

Even more surprisingly (for me), if I do something super-dirty like storing a reference to the Ninject Kernel instance in a public static property and use it as a ServiceLocator, it works! (Something dirty like this, in the MVC controller):

public MyController(IMyDearDependency something) { // 'something' is always null if Ninject is only in the GAC...
  var controller = Kernel.Get<MyController>()
  this.something = controller.something; // ... but this 'controller.something' is set, even if Ninject is only in the GAC!!! 8-O
}

Can anyone suggest me the reason why? And possibly a solution? :-) Many thanks!!

Was it helpful?

Solution

Ninject has a built in extension loading mechanism which is used to load the different extension like the Ninject.Web.Mvc.

But mechanism is looking only for the application folder to load the extensions so if your dll are in the GAC Ninject won't find them.

To solve this you can turn off the automatic extension loading and load the MvcModule module by hand when creating your StandardKernel:

var _kernel = new StandardKernel( 
    new NinjectSettings() { LoadExtensions = false }, 
    new MvcModule(),
    /* your other modules * /);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top