问题描述:

I'm testing Ninject, but following the how-to, i find it impossible to make it work. The information on the web is so messy even contradictory. I'm developping a website in MVC 4 on visual studio 2012 and i did install Ninject using Nuget.

So I get an error : "No parameterless constructor defined for this object.". As soon as I enter my controller.

I did the necessary steps :

  • Nuget installation
  • in NinjectWebCommon.cs, I did register my Interface in the RegisterServices method.
  • In my homecontroller I set my object like this :

    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 did I do wrong ? 4 Hours on it, I found that there may be a problem with my web.config file or that It could be a reference missing, or it could be a factory missing. Right now I don't even know what to do. Thanks for your help.

网友答案:

You must also install Ninject.MVC3 (https://www.nuget.org/packages/Ninject.MVC3/) which includes the Ninject Controller Factory, that takes care of instantiating the controllers and injecting the right dependencies into the controller constructor. I am almost certain that if you install Ninject.MVC3 through Nuget, everything gets configured automaticaly.

Without this package, MVC uses its default controller factory, that knows nothing about injecting dependencies.

网友答案:

Check if in your Web.config you have two lines for MVC versioning, 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 then replace them with one line like, to redirect to highest version that you are using in my case it's 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>

This did it for me.

相关阅读:
Top