C
C#2mo ago
blokyk

✅ Weird ASP0016 behavior

Is there a semantic difference between these two pieces of code? the first one is fine, but the second one gives me ASP0016 (which i don't think is correct? it's late and i'm kind of an asp noob so i might be mistaken...). the only difference is that the lambda for atomLinks.Select has async/await in the first one, but not in the second. Code 1, async/await, no warning
app.MapGet("/molecule.xml", async context => {
var atoms = await Task.WhenAll(
atomLinks.Select(
async (link) => await Utils.GetFeedAsync(link)
)
);
var molecule = Utils.Aggregate(atoms);

context.Response.ContentType = "application/atom+xml";
using var bodyXmlWriter = XmlWriter.Create(context.Response.Body);
molecule.SaveAsAtom10(bodyXmlWriter);
})
app.MapGet("/molecule.xml", async context => {
var atoms = await Task.WhenAll(
atomLinks.Select(
async (link) => await Utils.GetFeedAsync(link)
)
);
var molecule = Utils.Aggregate(atoms);

context.Response.ContentType = "application/atom+xml";
using var bodyXmlWriter = XmlWriter.Create(context.Response.Body);
molecule.SaveAsAtom10(bodyXmlWriter);
})
Code 2, no async/await, gives ASP0016
app.MapGet("/molecule.xml", async context => {
var atoms = await Task.WhenAll(
atomLinks.Select(
(link) => Utils.GetFeedAsync(link)
)
);
var molecule = Utils.Aggregate(atoms);

context.Response.ContentType = "application/atom+xml";
using var bodyXmlWriter = XmlWriter.Create(context.Response.Body);
molecule.SaveAsAtom10(bodyXmlWriter);
})
app.MapGet("/molecule.xml", async context => {
var atoms = await Task.WhenAll(
atomLinks.Select(
(link) => Utils.GetFeedAsync(link)
)
);
var molecule = Utils.Aggregate(atoms);

context.Response.ContentType = "application/atom+xml";
using var bodyXmlWriter = XmlWriter.Create(context.Response.Body);
molecule.SaveAsAtom10(bodyXmlWriter);
})
(weirdly, the warning also goes away if i eta-reduce the lambda into just the Utils.GetFeedAsync method group/name) (also: Compiler version: '5.0.0-2.25472.11 (b48cd5e8)'. Language version: 13.0. just in case)
ASP0016: Do not return a value from RequestDelegate
Learn about analysis rule ASP0016: Do not return a value from RequestDelegate
10 Replies
Big Jeff The Chef
There's some misunderstanding there about what await Task.WhenAll is doing firstly. As you are doing a linq select, but discarding the result. What you are doing is storing a completed task (that Task.WhenAll returns) but discarding the Select() result Hence your warning about discarding the delegate return
blokyk
blokykOP2mo ago
How is it discarding the select result? Doesn't .WhenAll return a Task<TResult[]> containing the results of each of the tasks?
Big Jeff The Chef
Doesn't it just retirn a single task denoting all tasks passed in have completed?
blokyk
blokykOP2mo ago
the docs say
Creates a task that will complete when all of the Task<TResult> objects in an enumerable collection have completed.
and it has this signature:
public static Task<TResult[]> WhenAll<TResult>(IEnumerable<Task<TResult>> tasks);
public static Task<TResult[]> WhenAll<TResult>(IEnumerable<Task<TResult>> tasks);
Big Jeff The Chef
Honestly I might be rambling it's 4am here 😅
blokyk
blokykOP2mo ago
I think you might be confusing it with Task.WaitAll? yep same timezone here, my eyes are barely staying open
Big Jeff The Chef
I'll circle back tomorrow when brain working again I'm curious myself now
blokyk
blokykOP2mo ago
it might be an analyzer bug honestly, these asp analyzer are a lil finicky in my experience lol
canton7
canton72mo ago
Looking at that analyzer, I have no idea why it would decide to fire there As you say, those two bits of code are effectively equivalent
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?