2012-09-14 2 views
3

데모 MVC 3 인터넷 응용 프로그램 템플릿을 사용하고 있으며 ServiceStack.Host.Mvc NuGet 패키지를 설치했습니다. Funq가 생성자 주입을 수행하는 데 문제가 있습니다.Constructor Injection with ServiceStack MVC Powerpack + Funq

다음 코드는 잘 작동 :

public class HomeController : ServiceStackController 
{ 
    public ICacheClient CacheClient { get; set; } 

    public ActionResult Index() 
    { 
     if(CacheClient == null) 
     { 
      throw new MissingFieldException("ICacheClient"); 
     } 

     ViewBag.Message = "Welcome to ASP.NET MVC!"; 

     return View(); 
    } 

    public ActionResult About() 
    { 
     return View(); 
    } 
} 

다음은 인터페이스의 인스턴스를 만들 수 없습니다 오류

에게 던졌습니다.

public class HomeController : ServiceStackController 
{ 
    private ICacheClient CacheClient { get; set; } 

    public ActionResult Index(ICacheClient notWorking) 
    { 
     // Get an error message... 
     if (notWorking == null) 
     { 
      throw new MissingFieldException("ICacheClient"); 
     } 

     CacheClient = notWorking; 

     ViewBag.Message = "Welcome to ASP.NET MVC!"; 

     return View(); 
    } 

    public ActionResult About() 
    { 
     return View(); 
    } 
} 

그것은 공공 재산 주입 작업 이후 큰 거래 아니지만, 내가 누락 무엇인지 알고 싶습니다. 당신의 두번째 예에서

+1

생성자는 어디에 있습니까? 그들은 나에게 똑같이 보입니까? – mythz

+0

그래, 그게 꽤 나쁜 것 ... 나는 분명히 ICacheClient 인터페이스를 생성자가 아닌 액션 메소드에 넣었다. 그 점을 지적 해 주신 고마워요. –

답변

1

주 당신은 생성자이없는하지만 당신은 방법이 수행에만 생성자와 공용 속성 작동하지 않습니다

public ActionResult Index(ICacheClient notWorking) 
{ 
    .... 
} 

가 주입됩니다. 다음과 같이 변경할 수 있습니다.

public class HomeController : ServiceStackController 
{ 
    private ICacheClient CacheClient { get; set; } 

    public HomeController(ICacheClient whichWillWork) 
    { 
     CacheClient = whichWillWork; 
    } 

    ... 
}