vNext Owin中间件

Sua*_*ere 1 middleware owin asp.net-core

我有一个简单的中间件:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

app.UseMiddleware<MiddlewareInterceptor>();
Run Code Online (Sandbox Code Playgroud)

上面的构建和应用程序似乎运行正常,但我在拦截器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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

app.Use(next => new MiddlewareInterceptor(next).Invoke);
Run Code Online (Sandbox Code Playgroud)

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

最后,我在Configure方法中直接尝试了一个内联函数,它也永远不会遇到断点:

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

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

小智 5

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