引入了新 app.Use
重载。 如果调用 app.Use
但从未调用 next
中间件,现在会收到编译器错误 CS0121:
以下方法或属性之间的调用不明确:“UseExtensions.Use(IApplicationBuilder、Func HttpContext、Func<、Task>)”和“UseExtensions.Use(IApplicationBuilder、Func<HttpContext、RequestDelegate、Task>)”
若要解决此错误,请使用 app.Run
而不是 app.Use
。
有关讨论,请参阅 GitHub 问题 dotnet/aspnetcore#32020。
已引入的版本
ASP.NET Core 6.0
旧行为
app.Use(async (context, next) =>
{
await next();
});
或
app.Use(async (context, next) =>
{
await SomeAsyncWork();
// next not called...
});
新行为
现在可将 context
传递到 next
委托:
app.Use(async (context, next) =>
{
await next(context);
});
在中间件从不调用 app.Run
时,请使用 next
:
app.Run(async (context) =>
{
await SomeAsyncWork();
// next never called
});
更改原因
上一 Use
种方法为每个请求分配两个对象。 新的重载会避免这些分配,但对调用 next
中间件的方式稍有更改。
建议的措施
如果遇到编译错误,则意味着在未使用 app.Use
委托的情况下调用 next
。 切换到app.Run
以修复错误。
受影响的 API
没有。