在.net核心应用启动中使用MapWhen和ApplicationBuilder的组合?

问题描述 投票:1回答:1

部分分支启动是否可能?

举个例子,是否可能有类似的东西:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.AlwaysUseThisMiddleware();
    app.MapWhen(conditionA, appBuilder => {appBuilder.SometimesUseThisOne;})
    app.MapWhen(conditionB, appBuilder => {appBuilder.SometimesUseThisOtherOne;})

或者我需要将AlwaysUseThisMiddleware放在每个分支内吗?像这样:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.MapWhen(conditionA, appBuilder =>
    {
        appBuilder.AlwaysUseThisMiddleware(); // Duplicated
        appBuilder.SometimesUseThisOne;
    )
    app.MapWhen(conditionB, appBuilder =>
    {
        appBuilder.AlwaysUseThisMiddleware(); // Duplicated
        appBuilder.SometimesUseThisOtherOne;
    )
c# asp.net-core middleware startup
1个回答
0
投票

简答:

是。它会像你期望的那样工作。


实际上,当我们Use()一系列中间件时,我们正在注册一系列中间件,这些中间件将在处理请求时按顺序调用。

MapWhen()方法只不过是辅助方法that invokes the Use()MapWhen(predicate,configFn)做的是注册运行as below的东西:

if (predicate(context)){
    await branch(context);
} else {
    await _next(context);
}

因此,当我们调用MapWhen()时,我们正在注册另一个分支处理的中间件。

例如 :

app.UseMiddleware<AlwaysUseThisMiddleware>();                

app.MapWhen(ctx=>ctx.Request.Query["a"]=="1", appBuilder =>{
    appBuilder.UseMiddleware<SometimesUseThisOne>();
});

app.MapWhen(ctx=>ctx.Request.Query["b"]=="1", appBuilder =>{
    appBuilder.UseMiddleware<SometimesUseThisOtherOne>();
})

// ...

基本上,此代码以下列方式运行:

call  `AlwaysUseThisMiddleware`;

////////////////////////////////////
if (ctx.Request.Query["a"]=="1"){   
    call SometimesUseThisOne ;            
} else {
    //------------------------------------------
    if (ctx.Request.Query["b"]=="1"){
        call SometimesUseThisOtherOne ;
    } else {
        //##################################################
        await _next(context);  // call other middlewares ...
        //##################################################
    }
    //-----------------------------------------
}
////////////////////////////////////

或者,如果您愿意,也可以按如下方式重写:

call `AlwaysUseThisMiddleware` middleware

if(ctx.Request.Query["a"]=="1")           // go to branch 1
    call `SometimesUseThisOne` middleware

else if (ctx.Request.Query["b"]=="1")     // go to branch 2
    call `SometimesUseThisOtherOne` middleware 

else :
    ...

请注意,这里的分支被翻译为else if而不是if。并且中间件AlwaysUseThisMiddleware总是在branch1和branch2之前调用。

© www.soinside.com 2019 - 2024. All rights reserved.