gpt4 book ai didi

.net - 处理 Owin/Katana 和 Autofac 中的 DI 错误

转载 作者:行者123 更新时间:2023-12-03 07:54:08 24 4
gpt4 key购买 nike

我有一个使用 Autofac DI 集成的 WebApi2 Owin 自托管应用程序。最近我花了一些时间来解决中间件构造函数注入(inject)的问题,没有任何提示或任何有用的异常细节,并且想知道如何改进下面的代码以在所有情况下提供错误细节。我正在使用 recommended approach for global error handling但看起来它不能处理所有事情。考虑以下片段:
中间件:

public class ClientCertificateAuthenticationMiddleware : AuthenticationMiddleware<ClientCertificateAuthenticationOptions>
{
IClaimAuthorizationProvider claimProvider;

public ClientCertificateAuthenticationMiddleware(OwinMiddleware next, ClientCertificateAuthenticationOptions options, IClaimAuthorizationProvider claimProvider)
: base(next, options)
{
this.claimProvider = claimProvider;
}

protected override AuthenticationHandler<ClientCertificateAuthenticationOptions> CreateHandler()
{
return new ClientCertificateAuthenticationHandler(claimProvider);
}
}
应用程序构建器,DI 配置
HttpConfiguration config = new HttpConfiguration();
// error handling
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
config.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

// DI registrations
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<ClaimAuthorizationProvider>().As<IClaimAuthorizationProvider>().InstancePerLifetimeScope();
// missing registration causing the error
//builder.RegisterInstance(new ClientCertificateAuthenticationOptions());
builder.RegisterType<ClientCertificateAuthenticationMiddleware>().InstancePerRequest();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

// integrate Autofac into Owin pipeline
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(config);

appBuilder.UseWebApi(config);
使用此配置,我得到以下响应,没有 IExceptionHandlerIExceptionLogger被击中:

HTTP/1.1 500 Internal Server Error

Content-Length: 0

Server: Microsoft-HTTPAPI/2.0

Date: Mon, 19 Oct 2015 07:55:00 GMT


问题的原因很简单——缺少依赖注册(注释掉的那个),但是没有任何错误细节很难确定,所以 我的问题是:我如何配置应用程序,以便所有异常,绝对所有未处理的内容都记录详细信息? (我对 IExceptionHandler 的期待)

最佳答案

我认为全局(webapi)错误处理程序和错误记录器正在拦截 Webapi 层/中间件中的异常。对于所有其他中间件,例如上面的自定义身份验证,您必须注意错误处理/日志记录。

关于.net - 处理 Owin/Katana 和 Autofac 中的 DI 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com