Api异常过滤器用于autofacc

本文关键字:autofacc 用于 过滤器 异常 Api | 更新日期: 2023-09-27 18:15:19

我已经配置了webapi与IAutofacExceptionFilter以获得异常详细信息。在autoface .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才能从容器中解析控制器类型的服务。

Api异常过滤器用于autofacc

您应该这样做,使AutofacWebApiDependencyResolver成为您默认的Web API依赖解析器:

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

查看Autofac文档获取更多信息