-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Announcement: aspnet/Announcements#461
[New Use middleware overload, can cause compiler errors if you don't call next]
A new overload of app.Use has been introduced. If you were previously using app.Use but never calling the next middleware, you will now get a compiler error (CS0121). If that's the case you should use app.Run instead of app.Use.
Version introduced
ASP.NET Core 6.0-preview4
Old behavior
app.Use(async (context, next) =>
{
await next();
});or
app.Use(async (context, next) =>
{
await SomeAsyncWork();
// next never called
});New behavior
app.Use(async (context, next) =>
{
await next(context);
});Notice that you can now pass context to the next delegate.
app.Run(async (context) =>
{
await SomeAsyncWork();
// next never called
});Use app.Run when your middleware never calls next, otherwise you will get a compile error:
CS0121 The call is ambiguous between the following methods or properties: 'UseExtensions.Use(IApplicationBuilder, Func<HttpContext, Func, Task>)' and 'UseExtensions.Use(IApplicationBuilder, Func<HttpContext, RequestDelegate, Task>)'
Reason for change
The previous Use method would allocate 2 objects per request. The new overload avoids these allocations with a small change to how you invoke the next middleware.
Recommended action
If you get a compile error, it means you are calling app.Use without using the next delegate. Switch to app.Run to fix the error.
Category
ASP.NET
Affected APIs
Not detectable via API analysis
Issue metadata
- Issue type: breaking-change