【问题标题】:vNext Owin MiddlewarevNext Owin 中间件
【发布时间】:2015-06-18 22:00:24
【问题描述】:

我有一个简单的中间件:

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 作为参数,这与你的老RequestDelegate 不同。所以我的app.Use 实例化我的MiddlewareInterceptor 失败,因为next 的类型是RequestDelegate

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

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

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

【问题讨论】:

标签: owin asp.net-core middleware


【解决方案1】:

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

【讨论】:

  • 很高兴知道,谢谢。它确实在 UseMvc() 之后被交换了。你摇滚
猜你喜欢
  • 2015-04-03
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 2014-04-21
  • 1970-01-01
  • 2014-08-13
相关资源
最近更新 更多