创建城堡温莎控制器厂和GetControllerInstance不会被调用 [英] Creating a Castle Windsor Controller Factory and GetControllerInstance is never called
本文介绍了创建城堡温莎控制器厂和GetControllerInstance不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我介绍DI到我的MS MVC应用程序,我有麻烦从我的自定义控制器厂中实例化的控制器。看来,被覆盖的GetControllerInstance不会被调用。
I am introducing DI into my MS MVC application and I am having trouble getting the controllers instantiated from within my custom Controller Factory. It seems that the overridden "GetControllerInstance" is not being called.
谁能告诉我,我缺少的是什么?
Can someone tell me what I am missing?
我的器厂:
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:
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());
}
}
帮助吗?
推荐答案
有一个一揽子的NuGet使温莎集成到MVC应用程序中的摩擦少。
There is a Nuget package that makes Windsor integration into MVC application friction-less.
PM> Install-Package Castle.Windsor.Web.Mvc
这篇关于创建城堡温莎控制器厂和GetControllerInstance不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文