|
| 1 | +--- |
| 2 | +title: "Breaking change - Streaming HTTP responses enabled by default in browser HTTP clients" |
| 3 | +description: "Learn about the breaking change in .NET 10 Preview 3 where streaming HTTP responses are enabled by default in browser HTTP clients." |
| 4 | +ms.date: 4/7/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs/issues/45644 |
| 7 | +--- |
| 8 | + |
| 9 | +# Streaming HTTP responses enabled by default in browser HTTP clients |
| 10 | + |
| 11 | +Browser HTTP clients now enable streaming HTTP responses by default. Consequently, the <xref:System.Net.Http.HttpContent.ReadAsStreamAsync*?displayProperty=nameWithType> method now returns a `BrowserHttpReadStream` instead of a <xref:System.IO.MemoryStream>, which does not support synchronous operations. This may require updates to existing code that relies on synchronous stream operations. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 3 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +In browser environments such as WebAssembly (WASM) and Blazor, the HTTP client buffered the entire response by default. The <xref:System.Net.Http.HttpContent> object contained a <xref:System.IO.MemoryStream> unless you explicitly opted in to streaming responses using the `WebAssemblyEnableStreamingResponse` option. |
| 20 | + |
| 21 | +```csharp |
| 22 | +var response = await httpClient.GetAsync("https://example.com"); |
| 23 | +var contentStream = await response.Content.ReadAsStreamAsync(); // Returns MemoryStream |
| 24 | +``` |
| 25 | + |
| 26 | +## New behavior |
| 27 | + |
| 28 | +Streaming HTTP responses are now enabled by default. The <xref:System.Net.Http.HttpContent> no longer contains a <xref:System.IO.MemoryStream>. Instead, <xref:System.Net.Http.HttpContent.ReadAsStreamAsync*?displayProperty=nameWithType> returns a `BrowserHttpReadStream`, which does not support synchronous operations. |
| 29 | + |
| 30 | +```csharp |
| 31 | +var response = await httpClient.GetAsync("https://example.com"); |
| 32 | +var contentStream = await response.Content.ReadAsStreamAsync(); // Returns BrowserHttpReadStream |
| 33 | +``` |
| 34 | + |
| 35 | +## Type of breaking change |
| 36 | + |
| 37 | +This is a [behavioral change](../../categories.md#behavioral-change). |
| 38 | + |
| 39 | +## Reason for change |
| 40 | + |
| 41 | +This change supports use-cases around streaming <xref:System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsAsyncEnumerable*>. |
| 42 | + |
| 43 | +## Recommended action |
| 44 | + |
| 45 | +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. |
| 46 | + |
| 47 | +To disable streaming for individual requests, use the following: |
| 48 | + |
| 49 | +```csharp |
| 50 | +request.Options.Set(new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"), false); |
| 51 | +// or |
| 52 | +request.SetBrowserResponseStreamingEnabled(false); |
| 53 | +``` |
| 54 | + |
| 55 | +To disable streaming globally, set the environment variable `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` or add the following to your project file: |
| 56 | + |
| 57 | +```xml |
| 58 | +<WasmEnableStreamingResponse>false</WasmEnableStreamingResponse> |
| 59 | +``` |
| 60 | + |
| 61 | +> [!NOTE] |
| 62 | +> 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](https://github.com/dotnet/runtime/issues/97449). |
| 63 | +
|
| 64 | +## Affected APIs |
| 65 | + |
| 66 | +- <xref:System.Net.Http.HttpContent?displayProperty=fullName> |
| 67 | +- <xref:System.Net.Http.HttpContent.ReadAsStreamAsync*?displayProperty=fullName> |
0 commit comments