New Use middleware overload, can cause compiler errors if you don't call next #32020
Labels
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
breaking-change
This issue / pr will introduce a breaking change, when resolved / merged.
feature-http-abstractions
Milestone
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 usingapp.Use
but never calling thenext
middleware, you will now get a compiler error (CS0121). If that's the case you should useapp.Run
instead ofapp.Use
.Version introduced
ASP.NET Core 6.0-preview4
Old behavior
or
New behavior
Notice that you can now pass
context
to thenext
delegate.Use
app.Run
when your middleware never callsnext
, 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 thenext
delegate. Switch toapp.Run
to fix the error.Category
ASP.NET
Affected APIs
Not detectable via API analysis
Issue metadata
The text was updated successfully, but these errors were encountered: