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

22,220

Solution 1

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!

Solution 2

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.

Solution 3

Don't reinvent the wheel and just try Install-Package Ninject.MVC3

Solution 4

I got the same error message after getting the MVC3 from NuGet.
I don't know if you got your project setup the same way as I do but my web.config is auto generated depending on environment. The MVC from NuGet is adding rows (<runtime>) to the web.config and those rows were removed since my merging config-files wasn't setup correctly.

Regards,

Kim

Solution 5

The whole thing is just ridiculous, I switched to Autofac, tired of never one time being able to add Ninject to a project successfully. Web API, MVC4, MVC5 all had problems.

For MVC5 people, using Ninject MVC3, add the following to web.config app level:

 <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
Share:
22,220
Gup3rSuR4c
Author by

Gup3rSuR4c

Updated on February 21, 2020

Comments

  • Gup3rSuR4c
    Gup3rSuR4c about 4 years

    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

  • Matt Ruwe
    Matt Ruwe over 11 years
    I'm seeing the same problem. Hopefully one of the two (VS 2012 or Ninject) will fix the problem.
  • rkrdo
    rkrdo about 11 years
    Awesome! Man, you just saved my day. Thanks a lot! I spent time trying to fix the problem and this solved it. BTW I am really interested in knowing why this happened and what are you basically doing with this code.
  • Michael
    Michael over 10 years
    I had to add the <runtime> element to my web config. As well as ensure <httpRuntime> had the targetFramework="4.5" attribute. This was my runtime element. <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
  • Samuel
    Samuel about 10 years
    In my case, I must use code like this: "DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModul‌​e));" in the Start method of NinjectWebCommon. If I replace NinjectWebCommon by your sugestion, I don't know where to move this code. If I try to move it in global.asax, I receive an error that telling me it's to late in the process to register module in the application starting. This is why the big guy of Ninject suggest to me to stay with the solution using NinjectWebCommon.
  • Gup3rSuR4c
    Gup3rSuR4c about 10 years
    Make sure to have the <dependentAssembly> section in your Web.config. My projects run just fine with the local IIS and that setting. Of course, the binding redirect should be <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" /> now since MVC 5.1 is out.
  • liang
    liang about 10 years
    my mvc4 project is not missing this line, but it still doesn't work.
  • Rowan
    Rowan almost 10 years
    exactly what I needed!
  • Gup3rSuR4c
    Gup3rSuR4c almost 10 years
    The project I'm working on right now is for MVC 5 (5.1.2) and I have not had to use a controller factory. I just have the binding redirects and it all works perfectly fine. I'm also using a local IIS server as well. I did split the Ninject code out of the MVC project and into its' own project though.
  • Arunas
    Arunas over 9 years
    That fixed for me, except I was using Mvc 5.0.0.0
  • SandRock
    SandRock over 9 years
    This causes a warning with ASP MVC5, Ninject3.2, Ninject.MVC3: "Do not use Ninject as Service Locator". This solution looks like a workaround.
  • dbd
    dbd about 9 years
    Liked @Michael, I needed to add runtime as well as assemblybinding element. Ninject.MVC4
  • VivekDev
    VivekDev over 7 years
    This is the right answer. I did Install-Package Ninject.MVC5 and it worked.
  • Koo SengSeng
    Koo SengSeng over 6 years
    Nailed it. Just uninstall Ninject MVC 3, install it again, rebuild entire solution, run it and fixed
  • Vikram Singh Saini
    Vikram Singh Saini over 4 years
    I was missing the reference to Ninject.Web.Mvc and also there was no Ninject.Web.Mvc.dll in bin folder. Added reference and issue resolved out.