我正在将 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。