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);
}