SportStore: WebUI.WindsorControllerFactory.GetControllerInstance (System.Type: no suitable method to override

trying to make his way through the MVC Steve Sandersons book, but hit a brick wall while creating a WindsorControllerFactory. It looks like the method has changed from MVC1 to MVC2. This is the error I get when trying to compile the project:

'WebUI.WindsorControllerFactory.GetControllerInstance (System.Type: no suitable method to override found). Any help would be appreciated - I can't get past this!

This is the code - as described in the book:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Castle.Windsor; using Castle.Windsor.Configuration.Interpreters; using Castle.Core.Resource; using System.Reflection; using Castle.Core; using Castle.MicroKernel; namespace WebUI { public class WindsorControllerFactory : DefaultControllerFactory { WindsorContainer container; // The constructor: // 1. Sets up a new IoC container // 2. Registers all components specified in web.config // 3. Registers all controller types as components public WindsorControllerFactory() { // Instantiate a container, taking configuration from web.config container = new WindsorContainer( new XmlInterpreter(new ConfigResource("castle")) ); // Also register all the controller types as transient var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t; foreach (Type t in controllerTypes) container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient); } // Constructs the controller instance needed to service each request protected override IController GetControllerInstance(Type controllerType) { return (IController)container.Resolve(controllerType); } } } 

++++ Regards, Martin

+7
asp.net-mvc castle-windsor
source share
2 answers

GetControllerInstance has changed from ASP.NET MVC 1.0 to ASP.NET MVC 2 due to an unsuccessful error regarding race conditions.

The signature in ASP.NET MVC 1.0 was:

 protected virtual IController GetControllerInstance( Type controllerType); 

And in ASP.NET MVC 2 this is:

 protected virtual IController GetControllerInstance( RequestContext requestContext, Type controllerType) 

In this particular case, it looks like you need to change the signature of your method to:

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

The main condition of the race was that the RequestContext instance could be used by several simultaneous requests, which would be a serious no-no. Fortunately, this does not look like all users have encountered this problem, but it has been fixed in ASP.NET MVC 2 anyway.

+14
source share

In MVC2, the signature of this method is as follows:

 protected internal virtual IController GetControllerInstance( RequestContext requestContext, Type controllerType ) 

(taken from MSDN .)

+1
source share

All Articles