Edit

Share via


Streaming HTTP responses enabled by default in browser HTTP clients

Browser HTTP clients now enable streaming HTTP responses by default. Consequently, the HttpContent.ReadAsStreamAsync method now returns a BrowserHttpReadStream instead of a MemoryStream, which does not support synchronous operations. This may require updates to existing code that relies on synchronous stream operations.

Version introduced

.NET 10 Preview 3

Previous behavior

In browser environments such as WebAssembly (WASM) and Blazor, the HTTP client buffered the entire response by default. The HttpContent object contained a MemoryStream unless you explicitly opted in to streaming responses using the WebAssemblyEnableStreamingResponse option.

var response = await httpClient.GetAsync("https://example.com");
var contentStream = await response.Content.ReadAsStreamAsync(); // Returns MemoryStream

New behavior

Streaming HTTP responses are now enabled by default. The HttpContent no longer contains a MemoryStream. Instead, HttpContent.ReadAsStreamAsync returns a BrowserHttpReadStream, which does not support synchronous operations.

var response = await httpClient.GetAsync("https://example.com");
var contentStream = await response.Content.ReadAsStreamAsync(); // Returns BrowserHttpReadStream

Type of breaking change

This is a behavioral change.

Reason for change

This change supports use-cases around streaming GetFromJsonAsAsyncEnumerable.

If your application relies on synchronous stream operations, update the code to use asynchronous alternatives. To disable streaming globally or for specific requests, use the provided configuration options.

To disable streaming for individual requests, use the following:

request.Options.Set(new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"), false);
// or
request.SetBrowserResponseStreamingEnabled(false);

To disable streaming globally, set the environment variable DOTNET_WASM_ENABLE_STREAMING_RESPONSE or add the following to your project file:

<WasmEnableStreamingResponse>false</WasmEnableStreamingResponse>

Note

As of .NET 10 Preview 3 the <WasmEnableStreamingResponse> property is not yet available. It will be available in a future release. For more details, see the GitHub issue.

Affected APIs