【发布时间】: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