0

I'm creating an Azure Function that returns chunked response (using await response.WriteAsync(...) ). When I open the Azure Function URL in Chrome I'm getting the error:  Chunked:1 GET http://localhost:7071/api/Chunked net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

This error happens most of the times, but sometime I'm getting the correct result. However, when I open the same URL from Edge, the response is always loaded correctly. I'm running the code locally from Visual Studio 15.8.7, Function Runtime Version: 2.0.12134.0,

I created a minimal repro:

[FunctionName("Chunked")]
public static async Task Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    var res = req.HttpContext.Response;
    res.StatusCode = 200;
    var token = " Lorem ipsum ";
    await res.WriteAsync(token);

    for (int i = 0; i < 100; i++)
    {
        token += token;
        if (token.Length > 2000)
            token = " Lorem ipsum ";
        await res.WriteAsync(i + token + "\n");         
        await Task.Delay(10); // Simulate wait for another record
    }
}

My code is more complex because it reads the content from database, but I simulated it with Task.Delay.

Jovan MSFT
  • 13,232
  • 4
  • 40
  • 55
  • Seems not related to function runtime as I can't repro with your code. Have you tried [solutions](https://stackoverflow.com/questions/29894154/chrome-neterr-incomplete-chunked-encoding-error) related to Chrome. – Jerry Liu Oct 22 '18 at 03:19

1 Answers1

0

ASP.NET:

Try to set the req.HttpContext.Response.BufferOutput to false. Response buffering can prevent correct response chunking.

For ASP.NET Core this seems to fit:

private void DisableResponseBuffering(HttpContext context)
{
    IHttpBufferingFeature bufferingFeature = context.Features.Get<IHttpBufferingFeature>();
    if (bufferingFeature != null)
    {
        bufferingFeature.DisableResponseBuffering();
    }
}
Sebastian Achatz
  • 668
  • 4
  • 14
  • 1
    Unfortunately Azure Functions are created in .Net Core 2/.Net standard and Buffer out is not available there: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpresponse?view=aspnetcore-2.1. I cannot reproduce this in standard ASP.NET where I could set this value. – Jovan MSFT Oct 25 '18 at 21:33