1

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pete rudensky
  • 404
  • 1
  • 5
  • 9

2 Answers2

2

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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thanks, I will follow github issue. – Pete rudensky Feb 03 '22 at 13:23
  • "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"*](https://meta.stackexchange.com/a/131011). 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."* – Peter Mortensen Aug 06 '23 at 13:17
  • @PeterMortensen issue was fixed in some preview version but then some.changes were made that lead to the issue to reappear. Improved wording. – Guru Stron Aug 06 '23 at 13:18
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

Rivo R.
  • 1
  • 2