views:

68

answers:

3

Hi,

This is from a very good book by Steven Sanderson

I am trying to follow the chapter 4 and trying to setup IOC on my mvc code from the code sample of the book but its not working.

I follow the code from page 97 to page 101 where I set up Inversion of Control and run the code but I get the following error.

A dialog box opens trying to search the following file:

c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs

It seems like controllertype is null in the following line of code:

protected override IController GetControllerInstance(
    System.Web.Routing.RequestContext requestContext, 
    Type controllerType)
{
    return (IController)container.Resolve(controllerType);
}

The exception happens at the above return statement saying " {"Value cannot be null.\r\nParameter name: service"}"

This happens to be in WindsorContainerFactory.

Routes looks as follows:

public class MvcApplication : System.Web.HttpApplication
{
 public static void RegisterRoutes(RouteCollection routes)
 {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Products", action = "List", id = ""} // Parameter defaults
    );
 }

 protected void Application_Start()
 {
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
 }
}

Please help..

Thanks..

+1  A: 

You could try debugging the route.

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Chris
+2  A: 

Your method is probably being called for requests that are not served by your app. For example for favicon.ico.

Make sure your controllerType is not null. If it is just return null and bypass the code that you added.

Different kind of browsers will make different "extra" requests to your site depending on how they cache. Also, if you are hosting the site with IIS vs Visual Studio Dev Server these might catch the request before it gets to your (e.g. IIS will server a .jpg file without having to letting go to your controller but the VS Dev Server might not)

Hector
A: 

So far I have found the following links addressing the same exact issue. I am copying them here for my own reference.

http://forums.asp.net/t/1559062.aspx?Pro+ASP.NET+MVC+-+setting+up+IOC+Castle+Windsor

dotnet-practitioner
this link totally worked for me.
dotnet-practitioner