我需要使用Nancy和Owin为Web应用配置Nhibernate,并希望根据请求注入并打开会话实例,在请求后关闭它并在出错时回滚。
只使用Nancy我会做这样的事情:
this.ApplicationPipelines.BeforeRequest += BeforeRequest;
this.ApplicationPipelines.AfterRequest += AfterRequest;
this.ApplicationPipelines.OnError += OnError;
答案 0 :(得分:0)
我没有使用NancyFX,但我认为你想要的是如下(使用Katana)
class RequestMiddleware : OwinMiddleware
{
public RequestMiddleware(OwinMiddleware next, IAppBuilder app)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
// Do stuff before passing on the request
await Next.Invoke(context);
// Do stuff after request/on response
if (context.Response.StatusCode == 500)
{
// Server had an error
}
}
}
您可以使用中间件来监视传入/传出请求,并根据请求或响应执行操作。你必须自己管理'捕捉'错误。