Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

UPDATE - Please look at my answer for a link and explanation of the solution to this problem

Before we start, I know this is a very common question and I've been using Ninject for many moons without issues, but now it's come up and I can't figure out a fix. Also, no, none of the results on Google and SO so far have helped me.

So, consider the following bit of code running on a very, very, very simple prototype ASP.NET MVC 4 project from Visual Studio 2012 on Windows Server 2008 R2:

public class DefaultController : Controller {     private IGroupPrincipalRepository GroupPrincipalRepository { get; set; }      [Inject]     public DefaultController(         IGroupPrincipalRepository groupPrincipalRepository) {         this.GroupPrincipalRepository = groupPrincipalRepository;     } } 

And here's the NinjectWebCommon.cs RegisterServices method:

kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(     c =>         new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "?", "?", "?", "?"))).InSingletonScope(); 

Now, this is how my other projects that use Ninject (but are ASP.NET MVC 3 on .NET 4) work and as far as I know this is what's needed to make everything work. So, why am I suddenly getting No parameterless constructor defined for this object. exceptions?

UPDATE

Here's the full NinjectWebCommon.cs file:

[assembly: WebActivator.PreApplicationStartMethod(typeof(App_Start.NinjectWebCommon), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(App_Start.NinjectWebCommon), "Stop")]  namespace App_Start {     using System;     using System.DirectoryServices.AccountManagement;     using System.Repositories.ActiveDirectory;     using System.Web;     using Microsoft.Web.Infrastructure.DynamicModuleHelper;     using Ninject;     using Ninject.Web.Common;      public static class NinjectWebCommon {         private static readonly Bootstrapper bootstrapper = new Bootstrapper();          public static void Start() {             DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));             DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));             bootstrapper.Initialize(CreateKernel);         }          public static void Stop() {             bootstrapper.ShutDown();         }          private static IKernel CreateKernel() {             var kernel = new StandardKernel();             kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);             kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();              RegisterServices(kernel);             return kernel;         }          private static void RegisterServices(             IKernel kernel) {             kernel.Bind(typeof(IGroupPrincipalRepository)).ToConstructor(                 c =>                     new GroupPrincipalRepository(new PrincipalContext(ContextType.Domain, "", "", "", ""))).InSingletonScope();         }     } } 

UPDATE - Please look at my answer for a link and explanation of the solution to this problem

like image 754
Gup3rSuR4c Avatar asked Sep 07 '12 03:09

Gup3rSuR4c


2 Answers

I know this is an old question but there don't seem to be any real answers and I've worked around the problem so here is my solution:

Create a custom controller factory:

public class NinjectControllerFactory : DefaultControllerFactory {     private IKernel ninjectKernel;     public NinjectControllerFactory(IKernel kernel)     {         ninjectKernel = kernel;     }     protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)     {         return (controllerType == null) ? null : (IController) ninjectKernel.Get(controllerType);     } } 

Then, if you are using NinjectHttpApplication, add the following line to OnApplicationStarted:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(Kernel)); 

If you aren't using NinjectHttpApplication, then add that line somewhere after you have created your kernel and pass it a reference to your freshly created kernel.

That's it.

like image 150
Jere.Jones Avatar answered Oct 04 '22 17:10

Jere.Jones


Well, I don't have an exact answer why the error is coming up, but I do know who is causing it and that is Visual Studio 2012. I installed Visual Studio 2010 on the same machine as 2012, installed ASP.NET MVC 4 for 2010 and I recreated the 2012 project into 2010 word for word, letter for letter. The final result is that when 2010 debugs the project everything works fine and Ninject injects the dependencies as it should.

When 2012 debugs its project it just comes up with the No parameterless constructor defined for this object exception. Re-targeting between .NET 4.0 and .NET 4.5 in 2012 doesn't do anything. Re-installing Ninject from NuGet also doesn't do anything. I even configured both 2010 and 2012 projects to use the local IIS server to be absolutely sure and the end result is the same.

I'm going to assume that there's a bug with Visual Studio 2012 or with Ninject. The only difference I've got between the two projects is which IDE they're running from and the 2012 project is the one that's crashing so that's why I'm pointing the finger at Visual Studio 2012.

UPDATE

Guys. GUYS! I ran into this problem AGAIN, and found the solution in another SO question: Ninject + MVC3 is not injecting into controller.

Basically, this is what's missing from the Web.config which makes it work:

<dependentAssembly>     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />     <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> 

I'm guessing this forces the framework to be aware of IoC containers which allows Ninject the finally be able to bind. Although, I can't help but think that the Ninject NuGet package should look for the existence of that binding redirect in the Web.config and auto-magically add it. It sure would help with a lot of hair pulling happening over this issue.

P.S. Up-vote the snot out of that post I linked because it deserves it!

like image 33
Gup3rSuR4c Avatar answered Oct 04 '22 17:10

Gup3rSuR4c