用于autofac的Api异常过滤器

时间:2015-09-30 08:58:10

标签: c# asp.net-web-api autofac

我已使用IAutofacExceptionFilter配置了webapi以获取异常详细信息。在autofac.config

中注册了所有内容
HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options, (configuration, builder) =>
{  
       builder.RegisterType<ErrorLoggingExceptionFilter>()
       .AsWebApiExceptionFilterFor<BaseController>()
       .InstancePerApiRequest();

       builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
       //builder.RegisterType<ErrorLoggingExceptionFilter>()
       //.AsWebApiExceptionFilterFor<BaseController>().SingleInstance();

       builder.OverrideWebApiActionFilterFor<BaseController>();
       builder.RegisterSource(
       new AnyConcreteTypeNotAlreadyRegisteredSource(t => 
         !(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Meta<>))
    ));
}
 var serviceResolver = (AutofacWebApiDependencyResolver)config.DependencyResolver;

但它给我的错误就像

  

AutofacWebApiDependencyResolver必须配置为Web API的依赖项解析程序,AutofacControllerConfigurationAttribute才能从容器中解析控制器类型的服务。

1 个答案:

答案 0 :(得分:0)

您应该执行以下操作以使AutofacWebApiDependencyResolver成为您的默认Web API依赖项解析程序:

// Set the dependency resolver to be Autofac.
var builder = new ContainerBuilder();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

有关详细信息,请参阅Autofac documentation