asp.net-mvcdependency-injectionninjectcontrollersexternal-assemblies

Using Ninject with MVC3 controllers that are in external assemblies


I have MVC3 controllers in external assemblies using MVC contrib's portable areas but I'm having a problem getting Ninject to inject the constructor dependencies.

The controller is found but I get an exception of "No parameterless constructor defined for this object". I can see that ninject's controller factory is being called in the call stack but for external assemblies it just does not pass my database session.

Why could this be?


Solution

  • You need to register the controllers in the other assemblies using Ninject's RegisterAllControllersIn method.

    This is my implementation in the applicationstarted method of global.asax:

            foreach (var ass in BuildManager.GetReferencedAssemblies().Cast<Assembly>())
            {
                RegisterAllControllersIn(ass);
            }