我尝试添加全局异常处理过滤器,该过滤器将记录MVC 5应用中的错误。我需要使用Autofac将错误记录服务注入过滤器以记录错误。我在这个应用程序中使用Autofac已经有一段时间没有问题地注入我的服务和存储库。
当我按照here和here概述的程序进行操作时,我遇到了一个空引用异常:
[NullReferenceException:对象引用未设置为的实例 对象。]
Autofac.Integration.Mvc.AutofacFilterProvider.GetFilters(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+206
System.Web.Mvc.FilterProviderCollection.GetFilters(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+182
System.Web.Mvc.ControllerActionInvoker.GetFilters(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+37 ...
我已在global.asax中删除了此属性的全局过滤器注册:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AuthorizeAttribute());
//filters.Add(new HandleExceptionAttribute());
}
设置我的Autofac依赖注册:
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(typeof(BaseService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.PropertiesAutowired();
builder.RegisterFilterProvider();
builder.RegisterType<HandleExceptionAttribute>()
.AsExceptionFilterFor<BaseController>()
.InstancePerHttpRequest()
.PropertiesAutowired();
我的Handle Exception Attribute with constructor injection:
public class HandleExceptionAttribute : HandleErrorAttribute
{
private IErrorLoggingService Service { get; set; }
public HandleExceptionAttribute(IErrorLoggingService service)
{
this.Service = service;
}
public override void OnException(ExceptionContext filterContext)
{
this.Service.LogError(new Error());
}
}
我已经在这个主题上做了很多谷歌搜索,但是找不到我正在寻找的确切例子。一些结果表明我也有安装Glimpse的问题,但我已经删除了。调用builder.RegisterFilterProvider(); Autofac设置内部是导致异常的行。
我错过了什么或这是不可能的?有没有人有一个有效的例子?谢谢!
编辑: 我应用了另一个全局过滤器(AuthorizeAttribute),我尝试删除它,但我仍然得到null ref异常。另一个想法是,我使用其他可能导致冲突的nuget包,就像Glimpse过去一样。 以下是我正在使用的其他nuget包:
<packages>
<package id="AmplifyJS" version="1.1.0" targetFramework="net45" />
<package id="angularjs" version="1.2.16" targetFramework="net45" />
<package id="Antlr" version="3.5.0.2" targetFramework="net45" />
<package id="Autofac" version="3.3.1" targetFramework="net45" />
<package id="Autofac.Mvc5" version="3.2.1" targetFramework="net45" />
<package id="CsQuery" version="1.3.4" targetFramework="net45" />
<package id="EntityFramework" version="6.1.0" targetFramework="net45" />
<package id="jQuery" version="2.1.0" targetFramework="net45" />
<package id="jQuery.Migrate" version="1.2.1" targetFramework="net45" />
<package id="jQuery.Validation" version="1.12.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="MicrosoftWebMvc" version="2.0" targetFramework="net45" />
<package id="MoxieManager" version="1.0" targetFramework="net45" />
<package id="Mvc2Futures" version="2.0.50217.0" targetFramework="net45" />
<package id="MvcContrib" version="2.0.95.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.2" targetFramework="net45" />
<package id="PreMailer.Net" version="1.2.4" targetFramework="net45" />
<package id="WebGrease" version="1.6.0" targetFramework="net45" />
</packages>
我会从头开始尝试一个MVC项目,因为你建议看看会发生什么。
答案 0 :(得分:2)
无法在空MVC5项目中重现您的问题。确保您使用的是正确的软件包,而且您的代码没有任何额外的有线员工。这是我的解决方案,工作正常。
packages.config:
<packages>
<package id="Autofac" version="3.3.1" targetFramework="net451" />
<package id="Autofac.Mvc5" version="3.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.1.0" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.1.0" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.1.0" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
</packages>
HandleExceptionAttribute:
public class HandleExceptionAttribute : HandleErrorAttribute
{
private IErrorLoggingService Service { get; set; }
public HandleExceptionAttribute(IErrorLoggingService service)
{
this.Service = service;
}
public override void OnException(ExceptionContext filterContext)
{
this.Service.LogError(filterContext.Exception);
}
}
IErrorLoggingService:
public interface IErrorLoggingService
{
void LogError(Exception error);
}
ErrorLoggingService:
public class ErrorLoggingService : IErrorLoggingService
{
public void LogError(Exception error)
{
Debug.WriteLine(error);
}
}
dependencyResolver replacement:
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.PropertiesAutowired();
builder.RegisterFilterProvider();
builder.RegisterType<HandleExceptionAttribute>()
.AsExceptionFilterFor<BaseController>()
.InstancePerHttpRequest()
.PropertiesAutowired();
DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
如果您的解决方案仍然无效,请尝试创建一个空项目并检查它是否正常工作。