2013-07-23 3 views
1
public partial class RegistrationController : Controller 
{ 

    private readonly IPartnerRepository _partnerRepository; 

    public RegistrationController(IPartnerRepository partnerRepository) 
    { 

     _partnerRepository =partnerRepository; 

    }} 

I의 의존성 주입에 autoface를 사용하고 난이 오류가 :오류 생성자 컨트롤러 MVC 4

public partial class RegistrationController : Controller 
{ 

    private readonly IPartnerRepository _partnerRepository; 

    public RegistrationController() 
    { 



    }} 
:
` 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) +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 a 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 
` 

난 생성자에서 삭제

내가되지이 오류가를

어떻게 해결할 수 있습니까?

+0

은 당신이 구현 저장소 인터페이스를 바인더 제본 적이뿐만 아니라. Autofac이 작업을 수행 할 수있는 충분한 지식을 제공해야한다고 생각이 물건에 대해 읽을 수 있습니까? –

답변

2

ASP.NET MVC에는 컨트롤러의 인스턴스를 만드는 특별한 서비스가 있습니다.이 서비스는 IControllerFactory 인터페이스를 구현해야합니다. IControllerFactory의 기본 구현에서는 컨트롤러에 매개 변수없는 생성자가 있고이를 호출 할 것으로 예상합니다. 당신이 컨트롤러와 DI를 사용하려면 두 가지 옵션이 있습니다

  1. 이 컨트롤러를 인스턴스화에 대한 귀하의 DI 컨테이너를 사용 IControllerFactory 당신 자신의 구현을 작성합니다. 다음과 같은 방법으로 등록 할 수 있습니다.

    var factory = new CustomControllerFactory (container); ControllerBuilder.Current.SetControllerFactory (factory);

  2. IDependencyResolver은 컨트롤러를 포함한 모든 MVC 인프라를 해결할 수 있습니다.

두 번째 옵션이 선호됩니다.

당신은 자세한 내용 here (는 유니티 컨테이너에 관하여하지만 난 당신에게

0

이것은 여러 가지 이유로 발생할 수 있습니다. 그 중 하나는 종속성 주입이 IPartnerRepository의 구체적인 클래스를 주입하도록 구성되어 있지 않다는 것입니다.

두 번째는 구체적인 클래스 자체에 삽입되는 다른 종속성에 의존하는 생성자가있는 경우이 두 번째 생성자는 종속성 삽입을 위해 구성되어 있지 않습니다.

마지막으로 이것은 주입되는 클래스가 포함 된 DLL을 찾을 수 없지만 일반적으로 주입 프레임 워크에서 DLL을 찾을 수 없다고 알려주는 경우에도 발생할 수 있습니다.