2013-08-25 1 views
0

저는 내 MVC 응용 프로그램에 DI를 도입하고 있으며 사용자 정의 컨트롤러 팩토리에서 컨트롤러를 인스턴스화하는 데 문제가 있습니다. 재정의 된 "GetControllerInstance"가 호출되지 않는 것 같습니다.성 Windsor 컨트롤러 팩터 리 만들기 및 GetControllerInstance가 호출되지 않습니다

누군가 내가 누락 된 내용을 말해 줄 수 있습니까?

내 컨트롤러 공장 :

public class WindsorControllerFactory : DefaultControllerFactory 
{ 
    readonly IWindsorContainer container; 

    public WindsorControllerFactory(IWindsorContainer container) 
    { 
     this.container = container; 
     var controllerTypes = 
      from t in Assembly.GetExecutingAssembly().GetTypes() 
      where typeof(IController).IsAssignableFrom(t) 
      select t; 
     foreach (var t in controllerTypes) 
      container.Register(Component.For(t).LifeStyle.Transient); 
    } 

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     if (controllerType == null) return null; 
     return (IController)container.Resolve(controllerType); 
    } 
} 

부트 스트랩 :

public static void Bootstrap() 
     { 
      RouteConfigurator.RegisterRoutes(RouteTable.Routes); 
      ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(IoC.Container)); 
      WindsorConfigurator.Configure(); 
     . 
     . 
     } 

IOC의 :

public static class IoC 
    { 
     private static readonly object LockObj = new object(); 

     private static IWindsorContainer container = new WindsorContainer(); 

     public static IWindsorContainer Container 
     { 
      get { return container; } 

      set 
      { 
       lock (LockObj) 
       { 
        container = value; 
       } 
      } 
     } 

     public static T Resolve<T>() 
     { 
      return container.Resolve<T>(); 
     } 

     public static object Resolve(Type type) 
     { 
      return container.Resolve(type); 
     } 
    } 

WindsorRegistrar :

public class WindsorRegistrar 
{ 

    public static void RegisterSingleton(Type interfaceType, Type implementationType) 
    { 
    IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).LifeStyle.Singleton); 
    } 

    public static void Register(Type interfaceType, Type implementationType) 
    { 
    IoC.Container.Register(Component.For(interfaceType).ImplementedBy(implementationType).LifeStyle.PerWebRequest); 
    } 

    public static void RegisterAllFromAssemblies(string a) 
    { 
    IoC.Container.Register(AllTypes.FromAssemblyNamed(a).Pick().WithService.FirstInterface().LifestylePerWebRequest()); 
    } 
} 

도움주세요.

나는 첫눈에 어떤 실수를 발견 할 수 없습니다 ...하지만 확실히 배관이 중복
+0

컨테이너에 'WindsorControllerFactory'를 등록하지 않았습니다. 아마 당신에게 이런 문제가 생길 것입니다. – Steven

답변

1

MVC로 윈저 통합을 만드는 Nuget 패키지가있다 마찰이 적다.

PM> Install-Package Castle.Windsor.Web.Mvc