0

我正在将 MVC3 Web 项目转换为 MVC4。我创建了一个继承自 HandleErrorAttribute 的 ElmahHandleErrorAttribute,它在 MVC3 中运行良好,并且在我的本地开发机器上也运行良好,但它在测试环境中引发了以下异常......

[InvalidOperationException: The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.]
   System.Web.Mvc.GlobalFilterCollection.ValidateFilterInstance(Object instance) +110
   System.Web.Mvc.GlobalFilterCollection.AddInternal(Object filter, Nullable`1 order) +17
   System.Web.Mvc.GlobalFilterCollection.Add(Object filter) +12
   Home2Me.MvcApplication.RegisterGlobalFilters(GlobalFilterCollection filters) in c:\###\Home2Me\Global.asax.cs:16
   Home2Me.MvcApplication.Application_Start() in c:\###\Home2Me\Global.asax.cs:118

[HttpException (0x80004005): The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9249709
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +131
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253

[HttpException (0x80004005): The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9164336
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256

课堂并没有什么特别之处。它非常接近 Elmah 的标准。

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{

    /// <summary>
    /// Called when an exception occurs.
    /// </summary>
    /// <param name="context"></param>
    public override void OnException(ExceptionContext context)
    {
        base.OnException(context);
        if (!context.ExceptionHandled) return; // if unhandled, will be logged anyhow
        var e = context.Exception;
        HandleException(e);
    }

... (code snipped for brevity) ...

}

这是在 Global.asax.cs 中注册过滤器的代码...

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new ElmahHandleErrorAttribute());
}

过滤器在一个单独的程序集中定义,该程序集也更新到 MVC4。我有多个使用此过滤器的项目。其中一个在测试环境中工作正常,另一个有同样的问题。

关于什么可能是错的任何想法?我相当有信心项目中的所有 MVC3 引用都已正确更新,包括根目录和视图目录中的 web.config 文件。它在本地工作,我卸载了 MVC3 并删除了所有“额外”的 MVC3 dll。

4

1 回答 1

0

我相信我找到了答案。我在根 web.config 文件中添加了一个运行时部分,以将 MVC3 引用转换为 MVC4。

<configuration>
    ... (snipped for brevity) ...
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

我宁愿修复引用 MVC3 的任何内容,但我无法弄清楚那可能是什么。现在,这必须是一个足够好的答案。

于 2013-10-02T18:06:37.957 回答