Why is my Castle Windsor controller factory's GetControllerInstance() being called with a null value?

家住魔仙堡 提交于 2019-12-04 08:28:37

问题


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

回答1:


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.




回答2:


Had this problem when following the Pro ASP.NET MVC Framework book, added

routes.IgnoreRoute("favicon.ico");

to the routes in the global.asax.cs file and it works. See more here: serving favicon.




回答3:


regarding registration of all the controllers you'd usually do it like this:

container.Register(
   AllTypes.FromThisAssembly()
      .BasedOn<IController>()
      .Configure(c => c.Lifestyle.Transient)
);

See the documentation for more explanation of the API.




回答4:


Very late addition: The step-by-step tutorial at the windsor site seems good. It breaks down the creation of a controller and how it's loaded into the factory. It also covers the "favicon.ico" noise.

http://docs.castleproject.org/(S(0jvahybwt45sgwzwirpa3455))/Windsor.Windsor-tutorial-part-one-getting-Windsor.ashx



来源:https://stackoverflow.com/questions/1434900/why-is-my-castle-windsor-controller-factorys-getcontrollerinstance-being-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!