.net 6 asp.net response completes immediately on linux

Viewed 59

I am creating server side web api endpoint in C# with .net6. The endpoint streams files from disk into HTTP response. After the entire file is streamed I want to calculate download speed and report it somewhere else. On Windows this works as expected - download registration is executed after entire file is received in the browser. But on Linux my code reaches the "RegisterDownload" logic a fraction of a second after download starts. The download continues in the browser, but server code is done streaming it seems. Looks like the server hands off the HTTP communication to some other component or the streamed data is cached somewhere on the network (?).

My code looks like this:

var start = DateTime.UtcNow;
HttpContext.Features.Get<IHttpResponseBodyFeature>().DisableBuffering();

await Response.SendFileAsync(getFileResponse.filepath, startBytes, bytesToSend, cancelToken);
_logger.LogDebug($"Sending completed after {(_dateTime.UtcNow - start):g} - cancel is {cancelToken.IsCancellationRequested}");
             
var elapsed = DateTime.UtcNow.Subtract(start);
await RegisterDownload(elapsed);

After a 50MB download that took around 5 seconds in the browser I am getting this log output:

Sending completed after 0:00:00.08602 - cancel is False

What I also tried (without success):

  • sneding file in chunks (loop that reads from a FileStream into 1MB buffer then sends each chunk into Response.Body.WriteAsync)
  • adding await Response.FlushAsync inside the loop and after loop completes
  • larger files (1GB)
0 Answers
Related