12

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);
}
scottm
  • 27,829
  • 22
  • 107
  • 159
  • Why are you hacking the registration with reflection instead of using proper registration API ? – Krzysztof Kozmic Jul 25 '10 at 04:28
  • @Krzysztof - I was following Steve Sanderson's example. If you want to post a better way to register all classes that inherit from controller, please do so. – scottm Jul 26 '10 at 16:24

4 Answers4

26

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.

scottm
  • 27,829
  • 22
  • 107
  • 159
  • 1
    Thanks, I spent hours looking for this solution. – Geo Oct 02 '09 at 03:42
  • 1
    And me three. Just to note, for MVC 2.0, it's this return base.GetControllerInstance(requestContext, controllerType); – Gabe Mar 19 '10 at 01:00
  • Me four. I was able to lose it over this one... glad I found this. – macca1 May 26 '10 at 01:50
  • @Jedi, For this specific problem, I just removed the reference to the script. If you are getting the error for an image or something, you can look into ignore routes – scottm Oct 21 '10 at 21:54
4

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.

Community
  • 1
  • 1
Artur Carvalho
  • 6,901
  • 10
  • 76
  • 105
  • Had the same problem as the guy asking the question and whilst better error handling is a good idea the actual issue was that the page was loading and then going to look for the favicon. Adding the line above solved my issue straight away. – colethecoder Feb 14 '11 at 10:42
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.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
0

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

J.Z.
  • 882
  • 9
  • 13