创建(无分支)OWIN 管道时,会考虑阶段标记。
但是,当使用分支管道(使用 app.Map
或 app.MapWhen
)时,每个分支中的所有中间件都在遇到的第一个阶段标记的阶段运行。
有什么方法可以在分支内使用阶段标记吗?
<小时/>为了说明问题,我使用 Katana branching pipeline demo 的修改版本
public void Configuration(IAppBuilder app)
{
app.Use<AddBreadCrumbMiddleware>("start-of-the-line");
app.Map("/branch1", app1 =>
{
app1.Use<AddBreadCrumbMiddleware>("took-branch1");
app1.Use<AddBreadCrumbMiddleware>("more-in-branch1");
// Nesting paths, e.g. /branch1/branch2
app1.Map("/branch2", app2 =>
{
app2.Use<AddBreadCrumbMiddleware>("took-branch2");
app2.Use<DisplayBreadCrumbs>();
});
app1.Use<DisplayBreadCrumbs>();
});
对于路径:/branch1/branch2
,这将输出预期结果:
start-of-the-line (stage: PreHandlerExecute), took-branch1 (stage: PreHandlerExecute), more-in-branch1 (stage: PreHandlerExecute), took-branch2 (stage: PreHandlerExecute)
现在我添加一些阶段标记:
app.Map("/branch1", app1 =>
{
app1.Use<AddBreadCrumbMiddleware>("took-branch1");
app1.UseStageMarker(PipelineStage.Authenticate);
app1.Use<AddBreadCrumbMiddleware>("more-in-branch1");
app1.UseStageMarker(PipelineStage.Authorize);
// Nesting paths, e.g. /branch1/branch2
app1.Map("/branch2", app2 =>
{
app2.Use<AddBreadCrumbMiddleware>("took-branch2");
app2.UseStageMarker(PipelineStage.PostResolveCache);
app2.Use<DisplayBreadCrumbs>();
});
app1.Use<DisplayBreadCrumbs>();
});
这次相同路径的输出是:
start-of-the-line (stage: Authenticate), took-branch1 (stage: Authenticate), more-in-branch1 (stage: Authenticate), took-branch2 (stage: Authenticate)
谁能解释为什么一切都在Authenticate
阶段运行,以及是否有解决方法来尊重分支中的其他阶段标记?
最佳答案
将评论作为答案显示:
By design. Stage markers are only supported in the root pipeline. Otherwise you may miss taking a branch. – Tratcher Oct 20 '15 at 1:19
所以,正如问题所述,“当使用分支管道(使用 app.Map
或 app.MapWhen
)时,每个分支中的所有中间件都在以下阶段运行:遇到的第一个阶段标记。”
在这种情况下,使用 SignalR 的注意事项:调用 SignalR OWIN 扩展之一(例如 MapSignalR
或 RunSignalR
)时,这些扩展会显式设置阶段标记以避免出现 session 状态:
// We need to make that SignalR runs before any handlers are
// mapped in the IIS pipeline so that we avoid side effects like
// session being enabled. [...]
builder.UseStageMarker(PipelineStage.PostAuthorize);
这意味着,如果您在 OWIN 应用中配置 SignalR,则所有请求(包括非 signalr 请求)都将在同一阶段运行,而无需 HttpContext.Current.Session
或 HttpContext.Current.CurrentHandler
.
关于asp.net - Owin 管道分支在遇到第一阶段标记时运行所有中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131387/