Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Use middleware overload, can cause compiler errors if you don't call next #32020

Closed
BrennanConroy opened this issue Apr 21, 2021 · 2 comments
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

Comments

@BrennanConroy
Copy link
Member

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
@ghost
Copy link

ghost commented Jun 21, 2021

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

@ghost ghost closed this as completed Jun 21, 2021
@BrennanConroy BrennanConroy reopened this Jun 21, 2021
@ghost
Copy link

ghost commented Aug 20, 2021

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

@ghost ghost closed this as completed Aug 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 19, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 2, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Projects
None yet
Development

No branches or pull requests

4 participants