我将 DI 引入到我的 MS MVC 应用程序中,并且无法从我的自定义控制器工厂中实例化控制器。似乎没有调用覆盖的“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();
.
.
}
国际奥委会:
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);
}
}
温莎注册商:
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());
}
}
请帮忙 ?