asp.net mvc - error constructor controller MvC 4 -
public partial class registrationcontroller : controller { private readonly ipartnerrepository _partnerrepository; public registrationcontroller(ipartnerrepository partnerrepository) { _partnerrepository =partnerrepository; }}
i use autoface dependancy injection , have error:
` no parameterless constructor defined object. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.missingmethodexception: no parameterless constructor defined object. source error: unhandled exception generated during execution of current web request. information regarding origin , location of exception can identified using exception stack trace below. stack trace: [missingmethodexception: no parameterless constructor defined 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) +55 [invalidoperationexception: une erreur s'est produite lors de la tentative de création d'un contrôleur de type « registration.front.web.controllers.registrationcontroller ». assurez-vous que le contrôleur un constructeur public sans paramètre.] system.web.mvc.defaultcontrolleractivator.create(requestcontext requestcontext, type controllertype) +179 system.web.mvc.defaultcontrollerfactory.getcontrollerinstance(requestcontext requestcontext, type controllertype) +80 system.web.mvc.defaultcontrollerfactory.createcontroller(requestcontext requestcontext, string controllername) +74 system.web.mvc.mvchandler.processrequestinit(httpcontextbase httpcontext, icontroller& controller, icontrollerfactory& factory) +197 system.web.mvc.mvchandler.beginprocessrequest(httpcontextbase httpcontext, asynccallback callback, object state) +49 system.web.mvc.mvchandler.beginprocessrequest(httpcontext httpcontext, asynccallback callback, object state) +50 system.web.mvc.mvchandler.system.web.ihttpasynchandler.beginprocessrequest(httpcontext context, asynccallback cb, object extradata) +16 system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +301 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155 `
when delete constructor, haven't error:
public partial class registrationcontroller : controller { private readonly ipartnerrepository _partnerrepository; public registrationcontroller() { }}
how can resolve this?
asp.net mvc has special service responsible creating instance of controller - service should implement icontrollerfactory
interface. default implementation of icontrollerfactory
expects controller has parameterless constructor , invokes it. if want use di controllers have 2 possible options:
write own implementation of
icontrollerfactory
uses di container instantiating controller. can register in such way:var factory = new customcontrollerfactory(container); controllerbuilder.current.setcontrollerfactory(factory);
implement
idependencyresolver
can resolve piece of mvc infrastructure including controllers.
second option preferrable.
you can read stuff more details here (it's unity container think should give enough knowledge autofac well.
Comments
Post a Comment