Тот код, что после await next() не вызывается в обоих случаях, так как в 35 строчке падает исключение. Вывод в браузере "To next middleware.To next middleware." Я ожидаю "To next middleware.To next middleware.After next middleware.After next middleware."
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
app.Use(async (ctx, next) =>
{
await ctx.Response.WriteAsync("To next middleware.");
await next(); //28 line
await ctx.Response.WriteAsync("After next middleware.");
});
app.Use(async (ctx, next) =>
{
await ctx.Response.WriteAsync("To next middleware.");
await next(); //35 line
await ctx.Response.WriteAsync("After next middleware.");
});
}
Под отладкой заметил, что в консоли вывода имеется такое сообщение с исключением. Попробовал выставить у ctx.Response.StatusCode это не привело к изменению поведении.
System.InvalidOperationException: StatusCode cannot be set because the response has already started.
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ThrowResponseAlreadyStartedException(String name)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.set_StatusCode(Int32 value)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.set_StatusCode(Int32 value)
at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_StatusCode(Int32 value)
at Microsoft.AspNetCore.Builder.ApplicationBuilder.<>c.<Build>b__18_0(HttpContext context)
at Microsoft.AspNetCore.Builder.UseExtensions.<>c__DisplayClass0_2.<Use>b__2()
at Platform.Startup.<>c.<<Configure>b__1_1>d.MoveNext() in C:\Users\illub\Desktop\learn\ProNetCore3\Ch12_Platform\Platform\Startup.cs:line 35
--- End of stack trace from previous location where exception was thrown ---
at Platform.Startup.<>c.<<Configure>b__1_0>d.MoveNext() in C:\Users\illub\Desktop\learn\ProNetCore3\Ch12_Platform\Platform\Startup.cs:line 28
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
В чем мое непонимание?