Ninject "No parameterless constructor defined for this object."

I am testing Ninject, but following the instructions, I find it impossible to get it to work. Information on the Internet is so dirty that it is even contradictory. I am developing a website in MVC 4 at visual studio 2012 and I installed Ninject using Nuget.

So, I get an error: "A constructor without parameters is not defined for this object." As soon as I enter the controller.

I took the necessary steps:

  • Install Nuget
  • in NinjectWebCommon.cs, I registered my interface in the RegisterServices method.
  • In my home controller, I set my object as follows:

    public ISurvey _survey { get; set; } [Inject] public HomeController(ISurvey s) { _survey = s; } 

And I get the following error message:

  Server Error in '/' Application.

 No parameterless constructor defined for this object.

 Description: An unhandled exception occurred during the execution of the current web request.  Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

 Source Error: 

 An unhandled exception was generated during the execution of the current web request.  Information regarding the origin and location of the exception can be identified using the exception stack trace below.

 Stack Trace: 


 [MissingMethodException: No parameterless constructor defined for this object.]
    System.RuntimeTypeHandle.CreateInstance (RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean & canBeCached, RuntimeMethodHandleInternal & ctor, Boolean & bNeedSecurityCheck) +0
    System.RuntimeType.CreateInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & ​​stackMark) +113
    System.RuntimeType.CreateInstanceDefaultCtor (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark & ​​stackMark) +232
    System.Activator.CreateInstance (Type type, Boolean nonPublic) +83
    System.Activator.CreateInstance (Type type) +6
    System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +110

 [InvalidOperationException: An error occurred when trying to create a controller of type 'Surveys.Controllers.HomeController'.  Make sure that the controller has a parameterless public constructor.]
    System.Web.Mvc.DefaultControllerActivator.Create (RequestContext requestContext, Type controllerType) +247
    System.Web.Mvc.DefaultControllerFactory.GetControllerInstance (RequestContext requestContext, Type controllerType) +438
    System.Web.Mvc.DefaultControllerFactory.CreateController (RequestContext requestContext, String controllerName) +226
    System.Web.Mvc.MvcHandler.ProcessRequestInit (HttpContextBase httpContext, IController & controller, IControllerFactory & factory) +326
    System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContextBase httpContext, AsyncCallback callback, Object state) +177
    System.Web.Mvc.MvcHandler.BeginProcessRequest (HttpContext httpContext, AsyncCallback callback, Object state) +88
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest (HttpContext context, AsyncCallback cb, Object extraData) +50
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +301
    System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +155 

So I wonder what I did wrong? 4 hours, I discovered that there may be a problem with my web.config file or that it may be a missing link, or it may be a factory missing. Right now I don’t even know what to do. Thank you for your help.

+5
Nov 21 '13 at 17:29
source share
2 answers

You should also install Ninject.MVC3 ( https://www.nuget.org/packages/Ninject.MVC3/ ), which includes the Ninject Factory controller, which takes care of instantiating the controllers and injecting the correct dependencies into the controller constructor. I am pretty sure that if you install Ninject.MVC3 through Nuget, everything will be automatically configured.

Without this package, MVC uses its default Factory controller, which knows nothing about dependency injection.

+1
Mar 15 '14 at 1:18
source share

Check if there are two lines in your Web.config for MVC version control, I had something like:

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

If you do, replace them with a single line, for example, to redirect to the highest version you use in my case, 4.0.0.1 :

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

It did it for me.

0
Dec 15 '14 at 9:33
source share



All Articles