处理 Owin/Katana 和 Autofac 中的 DI 错误
Handle DI errors in Owin/Katana and Autofac
我有一个使用 Autofac DI 集成的 WebApi2 Owin 自托管应用程序。最近我花了一些时间在没有任何提示或任何有用的异常详细信息的情况下解决中间件构造函数注入的问题,并且想知道我应该如何改进下面的代码以在所有情况下提供错误详细信息。我正在使用 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);
使用此配置,我得到以下响应,但没有命中 IExceptionHandler
和 IExceptionLogger
:
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 中的异常 tier/middleware。对于所有其他中间件,如上面的自定义身份验证,您必须注意错误 handling/logging.
我有一个使用 Autofac DI 集成的 WebApi2 Owin 自托管应用程序。最近我花了一些时间在没有任何提示或任何有用的异常详细信息的情况下解决中间件构造函数注入的问题,并且想知道我应该如何改进下面的代码以在所有情况下提供错误详细信息。我正在使用 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);
使用此配置,我得到以下响应,但没有命中 IExceptionHandler
和 IExceptionLogger
:
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 中的异常 tier/middleware。对于所有其他中间件,如上面的自定义身份验证,您必须注意错误 handling/logging.