최근에 StructureMap을 사용하여 종속성 주입을 구현하려고했습니다. 예제를 계속 따라갈 수 있었지만 응용 프로그램을 실행할 때마다 예외가 발생했습니다. 컨트롤러 팩토리에서 가져온 일부 코드입니다.ASP.NET MVC의 사용자 지정 컨트롤러 팩토리, Dependency Injection/Structuremap 문제
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null)
throw new ArgumentNullException("controllerType");
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
내 Global.asax에 내 모든 종속성을 등록하는 정적 부트 스트 래퍼 클래스를 호출하지만 StructureMapControllerFactory의 GetControllerInstance가 두 번 호출지고 것 같습니다. 처음 호출되면 적절한 controllerType이 전달되지만 두 번째 호출에서는 항상 null입니다.
처음으로 GetControllerInstance가 호출되면 형식이 올바른 HomeController로 설정됩니다. HomeController에서 발생하는 코드는 View()를 반환합니다. View()가 반환되면 aspx 파일에서 Page_Load 이벤트가 호출됩니다. 단계별로 진행되면 다음 행에 도착합니다.
httpHandler.ProcessRequest (HttpContext.Current);
두 번째로 GetControllerInstance가 호출됩니다.
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
Bootstrapper.ConfigureStructureMap();
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}
나는 의존성 주입 작업을 얻을 촉각을 곤두 세우고있어 누군가가 나를 도울 수 있다면 가장 감사 할 것입니다 :
여기에 관련이있을 수 내 Global.asax에 비트입니다. :)
안녕하세요, 동일한 문제가 발생했습니다. – Micah
컨트롤러를 어떻게 등록합니까? – Sebastian
좋은 문제 설명. –