我已使用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
才能从容器中解析控制器类型的服务。
答案 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。