views:

1883

answers:

3

I have a project for which I use StructureMap for dependency injection. The project compiles fine as a MVC project but after moving everything to a MVC2 project I am now receiving the following error:

Test.Web.Controllers.StructureMapControllerFactory.GetControllerInstance(System.Type)': no suitable method found to override C:\Test\Web\Controllers\StructureMapControllerFactory.cs 11 40 Test.Web

Here is my StructureMapControllerFactory:

using System;
using System.Web.Mvc;
using StructureMap;

namespace Test.Web.Controllers
{
    public class StructureMapControllerFactory : DefaultControllerFactory
    {

        protected override IController GetControllerInstance(Type controllerType)**
        {

            IController result = null;
            try
            {
                if (controllerType == null) return base.GetControllerInstance(controllerType);
                result = ObjectFactory.GetInstance(controllerType) as Controller;

            }
            catch (StructureMapException)
            {
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }

            return result;
        }

    }
}

I have found one post semi-related to this issue but it did not offer any insight as to how to resolve my issue: MVC 2 preview 1 - methods with parameters in the controller fail to load

Obviously I must be missing a change from the 1.0-2.0 progression, but I am not sure what changed. Any help is always appreciated.

+16  A: 

The signature of this method changed. There is now a first argument of RequestContext:

protected override IController GetControllerInstance(
    RequestContext requestContext, 
    Type controllerType)

You'll also need to change your call to base.GetControllerInstance:

if (controllerType == null) 
    return base.GetControllerInstance(requestContext, controllerType);
Craig Stuntz
Thanks so much Craig. I wonder why this is not reflected in the VS object browser when exploring the System.Web.MVC [2.0.0.0] namespace.
gun_shy
Not sure, but you can type ov[spacebar] in the code editor (i.e., start typing "override" where you would normally declare a new method) to get the list of overloadable methods for the current class with their signatures.
Craig Stuntz
+1  A: 

I traced it with the Reflector, and indeed the function signature is changed. the

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

the MVC 2 dll resides here: C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 2\Assemblies\System.Web.Mvc.dll

Thanks, it solved my issue!

Roman
+5  A: 

Craig Stuntz is very correct here.

Just make sure you don't forget to reference System.Web.Routing in addition if your DI is in a different project than you MVC app.

For some reason there were no errors showing in the IDE for me but on compile I would still get a GetControllerInstance "no suitable method found to override."

Once I corrected the missing referenced assembly of System.Web.Routing, all was well...

Jeff Spicoli
Thanks!. I don't know why yet, but adding a reference to System.Web.Routing fixed this for me!
Dave Van den Eynde