我正在将DI引入我的MS MVC应用程序中,并且我无法从我的自定义Controller Factory中实例化控制器。似乎没有调用被覆盖的“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 :(得分:1)
有一个Nuget软件包可以使Windsor集成到MVC应用程序中。
PM> Install-Package Castle.Windsor.Web.Mvc
答案 1 :(得分:0)
我无法在第一眼看到任何错误......但是肯定你的管道是多余的...按照windsor tutorial
保持简单