vNext Owin 中间件

vNext Owin Middleware

我有一个简单的中间件:

public class MiddlewareInterceptor
{
    RequestDelegate _next;
    public MiddlewareInterceptor(RequestDelegate next)
    {
        _next = next;
    }

    public Task Invoke(HttpContext ctx)
    {
        ctx.Response.WriteAsync("<h2>From SomeMiddleWare</h2>");
        return _next(ctx);
    }
}

在我的 Startup.cs 配置方法中,我像这样挂钩它:

app.UseMiddleware<MiddlewareInterceptor>();

以上构建和应用似乎 运行 正常,但我在拦截器 Invoke 方法中的断点从未命中。同样,永远不会有任何输出。我也用 Debug.WriteLine 试过了。

现在,我也试过这个方法:

public class MiddlewareInterceptor : OwinMiddleware
{
    public MiddlewareInterceptor(OwinMiddleware next) : base(next){}

    public override async Task Invoke(IOwinContext context)
    {
        Debug.WriteLine(context.Request.Uri.ToString());
        await Next.Invoke(context);
    }
}

在我的 Startup.cs 配置方法中,我像这样挂钩它:

app.Use(next => new MiddlewareInterceptor(next).Invoke);

不幸的是,基础 OwinMiddleware 构造函数正在寻找下一个 OwinMiddleware 作为参数,这与 ye olde RequestDelegate 不同。所以我的 MiddlewareInterceptorapp.Use 实例化失败了,因为 next 的类型是 RequestDelegate.

最后,我直接在 Configure 方法中尝试了一个内联函数,它也从未命中断点:

app.Use(async (ctx, next) =>
{
    System.Diagnostics.Debug.WriteLine("Hello");
    await next();
});

就目前而言,我似乎无法使用 OWIN 制作基本的中间件拦截器。我错过了什么?

上述中间件在管道中的顺序是什么?确保在终止管道的请求部分之前执行此操作;例如。使用Mvc();