I would like to create a standalone class library project that acts as my composition root. I want to keep the concrete implementations of types out of projects where they are never intended to be used directly. This is not possible if I'm forced to construct my Ninject registrations inside my MVC web application. For example I would have to map: IAbstraction
to ImplementationThatIsNotValidForDirectUseInThisLayer
.
I have created the composition root specific project and added references to all my projects so that I can bind my abstractions to their corresponding concrete implementations. I have installed the Ninject.MVC5 Nuget package (and all of its dependencies) into the composition root specific project.
When I reference my composition root project in my main MVC5 web application I can set a break point on the Start and Stop methods and the code executes. This means WebActivator
is correctly bootstrapping these classes at run-time.
The problem is when I hit a controller that is not parameterless I get the dreaded: "No parameterless constructor defined for this object." error message.
Why isn't Ninject being used to construct controllers and all of their dependencies? Is there some additional configuration I need to do?
I have a project reference from MyApp.WebApp
to the MyApp.WebApp.CompositionRoot.Ninject
project where all of my bindings are being configured.
The problem seems to occur only when the initial ASP.NET project is created from the Empty project template in Visual Studio (I'm using 2013 for what its worth) with MVC added via Nuget. If I create my ASP.NET web application using the MVC template and keep my composition root in an external class library referenced by the MVC application, the controllers are correctly constructed by Ninject. I've looked at the project created from the MVC template and the only reference that sounded potentially important that was missing was "System.Web.Abstractions. I added the reference but it did not fix the issue.