Why does 'foo' return an empty body?
Asked Answered
S

2

3

For some reason, foo always returns an empty body:

internal static async Task<string> Foo(HttpContext context)
{
    var response = await Task.Run(() => { return "response"; });
    return response;
}

internal static async Task<string> Bar(HttpContext context, string someParam)
{
    var response = await Task.Run(() => { return "response"; });
    return response;
}
Satisfactory answered 2/2, 2022 at 22:30 Comment(5)
What is the purpose of doing this?Excursive
Adding mapping: app.MapPost("/Foo", Handlers.Foo); app.MapPost("/Bar", Handlers.Bar);Satisfactory
I've noticed that Foo is tinted yellow and Bar is tinted white in the mappingsSatisfactory
@ŁukaszKomosa I have a bug that one api function is not returning anything. I'm trying to figure out why.Satisfactory
@Rivo R. That doesn't explain why Bar does return the response. You'll have to add these functions to a .Net 6 minimal web api project to reproduce the problem. I think it's a framework bug.Satisfactory
M
3

I was able to reproduce the behaviour. Moving the handler with the single parameter of HttpContext into a separate method leads to the empty response:

WebApplicationBuilder builder = WebApplication.CreateBuilder();
WebApplication app = builder.Build();
app.Map("/Fails", Fails);
app.Map("/Fails1", Fails1);
app.Map("/Works", async (HttpContext c) =>
{
    var response = await Task.Run(() => { return "response"; });
    return response;
});
app.Map("/WorksToo", Works);
app.Map("/WorksToo1", Works1);
app.Map("/WorksToo2", Works2);
app.Run();

static async Task<string> Fails1(HttpContext context)
{
    var response = await Task.FromResult("response");
    return response;
}

public partial class Program
{
    internal static async Task<string> Fails(HttpContext context) => await Task.FromResult("response");

    internal static async Task<string> Works(HttpContext context, string someParam) => await Task.FromResult("response");

    internal static async Task<string> Works1(HttpContext context, ILogger<Program> _) => await Task.FromResult("response");

    internal static async Task<string> Works2(HttpRequest context) => await Task.FromResult("response");
}

I submitted a new issue on GitHub. For now, you can add a dummy parameter (for example, CancelationToken) to the handler as I do with Works1.

Update 1

The issue was fixed and everything should work as expected in .NET 7.

Update 2

The issue was introduced back by some following changes and actually got worse. Follow the new issue at GitHub.

Maleki answered 3/2, 2022 at 13:3 Comment(3)
Thanks, I will follow github issue.Satisfactory
"Update 1" and "Update 2" seem contradictory. In any case, it would be better for the answer to be as if it was written today, without "update". E.g., past tense can be used for things that are no longer so. Or better version ranges (incl. release dates) for version-dependent statements. E.g., "Prior to version 1.7.7 (2017-12-19), A, B, and C. Between version 1.7.7 and 2.4.3, D, E, and F. For version 2.4.4 or later, G, H, and I."Dorita
@PeterMortensen issue was fixed in some preview version but then some.changes were made that lead to the issue to reappear. Improved wording.Maleki
B
0

Maybe you have to wait for the task to complete in order to see what is set in the Result property of the returned Task instance.

I wrote almost the same code in a console application (without the HttpContext parameter) and what I notice is that when you use the Result property in your code, the result returned by your Foo() method is there. It might trigger the task to wait for completion.

Link: https://www.pluralsight.com/guides/using-task-run-async-await

Brainy answered 2/2, 2022 at 22:59 Comment(1)
await should take care of setting the result property.Maleki

© 2022 - 2024 — McMap. All rights reserved.