You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use HttpClient's HttpCompletionOption.ResponseHeadersRead to be able to copy data to a buffer as it is streamed, and to use a Progress instance to track the progress of a download to display in the UI.
I noticed that this became orders of magnitude slower when we migrated to .NET Android from Xamarin. After investigating, whenever we call client.SendAsync(), it is slow whenever we are using ResponseHeadersRead as the completion option: await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri), HttpCompletionOption.ResponseHeadersRead);
Sample project attached showing both cases and the printed times with a sample 100mb file from a speed test (feel free to try with different files). Here is the data from making an API call in that sample project:
We are not experiencing this in net8-ios or net8-windows with the same code. This also only happens when using the Native HttpHandler (the default in net8-android and MAUI as far as I am aware).
Steps to Reproduce
Create a new Android project
Ensure that UseNativeHttpHandler is set to true in the csproj.
Make a request with the sample code and take note of the returned time from the Stopwatch:
Make the same call again, but remove HttpCompletionOption.ResponseHeadersRead (or set it to the default HttpCompletionOption.ResponseContentRead) and observe that the download time is significantly faster and en-par with other platforms.
Remove HttpCompletionOption.ResponseHeadersRead and remove this functionality; download still works, there is just no reporting or control as the input is streamed.
Android application type
.NET Android (net7.0-android, net8.0-android, etc.)
Affected platform version
VS 2022 17.9.4, .NET 8.0.200
Description
I use HttpClient's HttpCompletionOption.ResponseHeadersRead to be able to copy data to a buffer as it is streamed, and to use a Progress instance to track the progress of a download to display in the UI.
I noticed that this became orders of magnitude slower when we migrated to .NET Android from Xamarin. After investigating, whenever we call
client.SendAsync()
, it is slow whenever we are using ResponseHeadersRead as the completion option:await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri), HttpCompletionOption.ResponseHeadersRead);
Sample project attached showing both cases and the printed times with a sample 100mb file from a speed test (feel free to try with different files). Here is the data from making an API call in that sample project:
Sample Project: HttpResponseHeadersRepro.zip
await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri), HttpCompletionOption.ResponseHeadersRead);
await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
We are not experiencing this in net8-ios or net8-windows with the same code. This also only happens when using the Native HttpHandler (the default in net8-android and MAUI as far as I am aware).
Steps to Reproduce
UseNativeHttpHandler
is set to true in the csproj.HttpCompletionOption.ResponseHeadersRead
(or set it to the defaultHttpCompletionOption.ResponseContentRead
) and observe that the download time is significantly faster and en-par with other platforms.Did you find any workaround?
SetEdit: this does not work when compiling in Release with AOT as per Usage of SocketsHttpHandler has runtime exceptions with AOT/LLVM compilation on net8.0-android #8814UseNativeHttpHandler
in your csproj to false. This allows progress to be reported without any additional latencyHttpCompletionOption.ResponseHeadersRead
and remove this functionality; download still works, there is just no reporting or control as the input is streamed.Relevant log output
log.txt
The text was updated successfully, but these errors were encountered: