Skip to content

Analyzer: Detect using Task<T> with RequestDelegate #42523

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

Closed
1 task done
JamesNK opened this issue Jul 1, 2022 · 2 comments · Fixed by #44048
Closed
1 task done

Analyzer: Detect using Task<T> with RequestDelegate #42523

JamesNK opened this issue Jul 1, 2022 · 2 comments · Fixed by #44048
Assignees
Labels
analyzer Indicates an issue which is related to analyzer experience old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@JamesNK
Copy link
Member

JamesNK commented Jul 1, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

RequestDelegate can be used with methods/lambdas that return a value which is then ignored.

Delegate signature:

/// <summary>
/// A function that can process an HTTP request.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> for the request.</param>
/// <returns>A task that represents the completion of request processing.</returns>
public delegate Task RequestDelegate(HttpContext context);

Because Task is the base type of Task<T>, generic variance means it's possible to do this:

Task<string> HelloWorld(HttpContext c) => Task.FromResult("Hello " + c.Request.RouteValues["name"]);

// The compiler prefers RequestDelegate overload over the Delegate overload.
// The return value is never used. Not what the user expects.
endpoints.MapGet("/", HelloWorld);

Example: #39956

Describe the solution you'd like

Write an analyzer that detects using a method or lambda that returns Task<T> with RequestDelegate and warns the user.

Additional context

No response

@martincostello
Copy link
Member

Same as #36408?

@JamesNK
Copy link
Member Author

JamesNK commented Jul 4, 2022

No.

Your analyzer issue is for checking what's passed to Result.Ok (and friends).
This analyzer issue is about using a method that returns Task<T> with RequestDelegate.

@adityamandaleeka adityamandaleeka added old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Jul 6, 2022
@rafikiassumani-msft rafikiassumani-msft added the analyzer Indicates an issue which is related to analyzer experience label Jul 7, 2022
@rafikiassumani-msft rafikiassumani-msft added this to the .NET 8 Planning milestone Jul 7, 2022
@JamesNK JamesNK self-assigned this Sep 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
analyzer Indicates an issue which is related to analyzer experience old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants