为什么我的温莎城堡控制器工厂的GetControllerInstance()被调用用null值? [英] Why is my Castle Windsor controller factory's GetControllerInstance() being called with a null value?
问题描述
我使用温莎城堡管理控制器实例(除其他事项外)。我的控制器工厂看起来是这样的:
公共类WindsorControllerFactory:DefaultControllerFactory
{
私人WindsorContainer _container; 公共WindsorControllerFactory()
{
_container =新WindsorContainer(新XmlInter preTER()); VAR controllerTypes从t =在Assembly.GetExecutingAssembly()。GetTypes()
其中,typeof运算(控制器).IsAssignableFrom(T)
选择吨; 的foreach(在controllerTypes T型)
{
_container.AddComponentLifeStyle(t.FullName,T,LifestyleType.Transient);
}
} 保护覆盖一个IController GetControllerInstance(类型controllerType)
{
回报(一个IController)_container.Resolve(controllerType); // ArgumentNullException在这里抛出
}
当我开始了我的ASP.Net MVC应用程序,并尝试去/(或其他路径),我得到一个ArgumentNullException。我把一个破发点上GetControllerInstance的条目,并发现它与我的HomeController中,再进行第二次的时间与空(也就是当抛出异常)调用一次。为什么它被再次调用?
我应该改变的方法是这样的:
保护覆盖一个IController GetControllerInstance(类型controllerType)
{
如果(controllerType == NULL)
返回null; 回报(一个IController)_container.Resolve(controllerType);
}
原来,第二个请求是MVC框架试图找到一个脚本我列入的Site.Master。该路径并不存在,所以我想它试图解决一个控制器(匹配/Scripts/sitescripts.js)。我改变了方法如下:
保护覆盖一个IController GetControllerInstance(类型controllerType)
{
如果(controllerType!= NULL)
{
回报(一个IController)_container.Resolve(controllerType);
}
其他
{
返回base.GetControllerInstance(controllerType);
}
}
和带有一个可以理解的消息抛出异常。
I am using Castle Windsor to manage controller instances (among other things). My controller factory looks like this:
public class WindsorControllerFactory : DefaultControllerFactory
{
private WindsorContainer _container;
public WindsorControllerFactory()
{
_container = new WindsorContainer(new XmlInterpreter());
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(Controller).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
{
_container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
}
protected override IController GetControllerInstance(Type controllerType)
{
return (IController)_container.Resolve(controllerType); // ArgumentNullException is thrown here
}
When I start up my ASP.Net MVC application and try to go to "/" (or another path), I get an ArgumentNullException. I put a break point on entry of the GetControllerInstance and found that it's called once with my HomeController, then a second time with null (which is when the exception is thrown). Why is it being called again?
Should I change the method to something like this:
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null)
return null;
return (IController)_container.Resolve(controllerType);
}
It turns out that the second request was the MVC framework trying to find a script I included in the Site.Master. The path did not exist, so I guess it tried to resolve a controller (that matched /Scripts/sitescripts.js). I changed the method to this:
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType != null)
{
return (IController)_container.Resolve(controllerType);
}
else
{
return base.GetControllerInstance(controllerType);
}
}
And an exception with an understandable message was thrown.
这篇关于为什么我的温莎城堡控制器工厂的GetControllerInstance()被调用用null值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!