Skip to content

Commit d217293

Browse files
authored
[Pre3] Response streaming default and opt-out (#34894)
1 parent 87f5f63 commit d217293

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

aspnetcore/blazor/call-web-api.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,54 @@ requestMessage.Headers.Add("X-Requested-With", [ "XMLHttpRequest" ]);
972972

973973
Blazor's client-side implementation of <xref:System.Net.Http.HttpClient> uses [Fetch API](https://developer.mozilla.org/docs/Web/API/fetch) and configures the underlying [request-specific Fetch API options](https://developer.mozilla.org/docs/Web/API/fetch#Parameters) via <xref:System.Net.Http.HttpRequestMessage> extension methods and <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions>. Set additional options using the generic <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestOption%2A> extension method. Blazor and the underlying Fetch API don't directly add or modify request headers. For more information on how user agents, such as browsers, interact with headers, consult external user agent documentation sets and other web resources.
974974

975-
The HTTP response is typically buffered to enable support for synchronous reads on the response content. To enable support for response streaming, use the <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> extension method on the request.
975+
:::moniker range=">= aspnetcore-10.0"
976+
977+
Response streaming is enabled by default.
978+
979+
Calling <xref:System.Net.Http.HttpContent.ReadAsStreamAsync%2A?displayProperty=nameWithType> for an <xref:System.Net.Http.HttpResponseMessage.Content%2A?displayProperty=nameWithType> (`response.Content.ReadAsStreamAsync()`) returns a [`BrowserHttpReadStream` (reference source)](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/BrowserHttpHandler.cs), not a <xref:System.IO.MemoryStream>. `BrowserHttpReadStream` doesn't support synchronous operations, such as `Stream.Read(Span<Byte>)`. If your code uses synchronous operations, you can opt-out of response streaming or copy the <xref:System.IO.Stream> into a <xref:System.IO.MemoryStream> yourself.
980+
981+
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
982+
983+
<!-- UPDATE 10.0 - Tracking on https://github.com/dotnet/runtime/issues/97449
984+
985+
To opt-out of response streaming globally, use either of the following approaches:
986+
987+
* Add the `<WasmEnableStreamingResponse>` property to the project file with a value of `false`:
988+
989+
```xml
990+
<WasmEnableStreamingResponse>false</WasmEnableStreamingResponse>
991+
```
992+
993+
* Set the `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` environment variable to `false` or `0`.
994+
995+
-->
996+
997+
To opt-out of response streaming globally, set the `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` environment variable to `false` or `0`.
998+
999+
To opt-out of response streaming for an individual request, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `false` on the <xref:System.Net.Http.HttpRequestMessage> (`requestMessage` in the following example):
1000+
1001+
```csharp
1002+
requestMessage.SetBrowserResponseStreamingEnabled(false);
1003+
```
1004+
1005+
:::moniker-end
1006+
1007+
:::moniker range="< aspnetcore-10.0"
1008+
1009+
The HTTP response is typically buffered to enable support for synchronous reads on the response content. To enable support for response streaming, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `true` on the <xref:System.Net.Http.HttpRequestMessage>:
1010+
1011+
```csharp
1012+
requestMessage.SetBrowserResponseStreamingEnabled(true);
1013+
```
1014+
1015+
By default, [`HttpCompletionOption.ResponseContentRead`](xref:System.Net.Http.HttpCompletionOption) is set, which results in the <xref:System.Net.Http.HttpClient> completing after reading the entire response, including the content. In order to be able to use the <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> option on large files, set [`HttpCompletionOption.ResponseHeadersRead`](xref:System.Net.Http.HttpCompletionOption) to avoid caching the file's content in memory:
1016+
1017+
```diff
1018+
- var response = await Http.SendAsync(requestMessage);
1019+
+ var response = await Http.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
1020+
```
1021+
1022+
:::moniker-end
9761023

9771024
To include credentials in a cross-origin request, use the <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestCredentials%2A> extension method:
9781025

aspnetcore/release-notes/aspnetcore-10/includes/blazor.md

+34
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,37 @@ The following example uses the `CloseColumnOptionsAsync` method to close the col
8989
movies.Where(m => m.Title!.Contains(titleFilter));
9090
}
9191
```
92+
93+
<!-- PREVIEW 3 ..... NOTE CONTENT CHANGE FOR `<WasmEnableStreamingResponse>` BELOW!!!!!
94+
95+
### Response streaming is opt-in and how to opt-out
96+
97+
In prior Blazor releases, response streaming for <xref:System.Net.Http.HttpClient> requests was opt-in. Now, response streaming is enabled by default.
98+
99+
This is a breaking change because calling <xref:System.Net.Http.HttpContent.ReadAsStreamAsync%2A?displayProperty=nameWithType> for an <xref:System.Net.Http.HttpResponseMessage.Content%2A?displayProperty=nameWithType> (`response.Content.ReadAsStreamAsync()`) returns a `BrowserHttpReadStream` and no longer a <xref:System.IO.MemoryStream>. `BrowserHttpReadStream` doesn't support synchronous operations, such as `Stream.Read(Span<Byte>)`. If your code uses synchronous operations, you can opt-out of response streaming or copy the <xref:System.IO.Stream> into a <xref:System.IO.MemoryStream> yourself.
100+
101+
DON'T USE (comment out) ..............
102+
103+
To opt-out of response streaming globally, use either of the following approaches:
104+
105+
* Add the `<WasmEnableStreamingResponse>` property to the project file with a value of `false`:
106+
107+
```xml
108+
<WasmEnableStreamingResponse>false</WasmEnableStreamingResponse>
109+
```
110+
111+
* Set the `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` environment variable to `false` or `0`.
112+
113+
..................... UNTIL https://github.com/dotnet/runtime/issues/97449 IS RESOLVED AND RELEASED.
114+
115+
To opt-out of response streaming globally, set the `DOTNET_WASM_ENABLE_STREAMING_RESPONSE` environment variable to `false` or `0`.
116+
117+
To opt-out of response streaming for an individual request, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserResponseStreamingEnabled%2A> to `false` on the <xref:System.Net.Http.HttpRequestMessage> (`requestMessage` in the following example):
118+
119+
```csharp
120+
requestMessage.SetBrowserResponseStreamingEnabled(false);
121+
```
122+
123+
For more information, see [`HttpClient` and `HttpRequestMessage` with Fetch API request options (*Call web API* article)](xref:blazor/call-web-api?view=aspnetcore-10.0#httpclient-and-httprequestmessage-with-fetch-api-request-options).
124+
125+
-->

0 commit comments

Comments
 (0)