10

我有一个项目,我使用 StructureMap 进行依赖注入。该项目作为 MVC 项目编译得很好,但是在将所有内容移动到 MVC2 项目后,我现在收到以下错误:

Test.Web.Controllers.StructureMapControllerFactory.GetControllerInstance(System.Type)':找不到合适的方法来覆盖 C:\Test\Web\Controllers\StructureMapControllerFactory.cs 11 40 Test.Web

这是我的 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;
        }

    }
}

我发现一篇与这个问题半相关的帖子,但它没有提供任何关于如何解决我的问题的见解:MVC 2 preview 1 - methods with parameters in the controller failed to load

显然,我一定错过了 1.0-2.0 进程的变化,但我不确定发生了什么变化。任何帮助总是受到赞赏。

4

3 回答 3

19

此方法的签名已更改。现在有 RequestContext 的第一个参数:

protected override IController GetControllerInstance(
    RequestContext requestContext, 
    Type controllerType)

您还需要更改对 base.GetControllerInstance 的调用:

if (controllerType == null) 
    return base.GetControllerInstance(requestContext, controllerType);
于 2009-09-01T20:07:41.397 回答
8

Craig Stuntz 在这里非常正确。

如果您的 DI 与 MVC 应用程序位于不同的项目中,请确保您不要忘记引用 System.Web.Routing。

出于某种原因,IDE 中没有为我显示任何错误,但在编译时我仍然会得到一个 GetControllerInstance“找不到合适的方法来覆盖”。

一旦我纠正了 System.Web.Routing 缺少的引用程序集,一切都很好......

于 2010-05-03T07:16:48.427 回答
1

我用反射器跟踪它,确实改变了函数签名。这

受保护的内部虚拟 IController GetControllerInstance(RequestContext requestContext, Type controllerType)

MVC 2 dll 位于:C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 2\Assemblies\System.Web.Mvc.dll

谢谢,解决了我的问题!

于 2009-10-14T11:03:16.537 回答