Search code examples
c#.net-6.0minimal-apis

Why does 'foo' return an empty body?


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;
}

Solution

  • 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.