Skip to content

Commit 04c37e4

Browse files
Apply suggestions from code review about MaxResponseContentBufferSize and rewording
Co-authored-by: Miha Zupan <[email protected]>
1 parent 1c8336f commit 04c37e4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,30 @@ class HttpCompletionOptionSnippets
99
public static async Task RunAsync()
1010
{
1111
//<SnippetHttpCompletionOption>
12-
var httpClient = new HttpClient();
12+
using var httpClient = new HttpClient();
1313
httpClient.Timeout = TimeSpan.FromSeconds(30);
14-
using (var response = await httpClient.GetAsync("http://localhost:12345/", HttpCompletionOption.ResponseHeadersRead)) // 30-second timeout but ONLY up until past the headers
15-
{
16-
// Do other stuff that doesn't rely on the content first, like status code validation
17-
response.EnsureSuccessStatusCode();
14+
httpClient.MaxResponseContentBufferSize = 1_000; // This will be ignored
1815

19-
var content = await response.Content.ReadAsStringAsync(); // NO TIMEOUT
20-
}
16+
// Because we're specifying the ResponseHeadersRead option,
17+
// the 30-second timeout applies only up until the headers are received.
18+
// It does not affect future operations that interact with the response content.
19+
using HttpResponseMessage response = await httpClient.GetAsync(
20+
"http://localhost:12345/",
21+
HttpCompletionOption.ResponseHeadersRead);
22+
23+
// Do other checks that don't rely on the content first, like status code validation.
24+
response.EnsureSuccessStatusCode();
25+
26+
// Since the HttpClient.Timeout will not apply to reading the content,
27+
// you must enforce it yourself, for example by using a CancellationTokenSource.
28+
using var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(15));
29+
30+
// If you wish to enforce the MaxResponseContentBufferSize limit as well,
31+
// you can do so by calling the LoadIntoBufferAsync helper first.
32+
await response.Content.LoadIntoBufferAsync(1_000, cancellationSource.Token);
33+
34+
// Make sure to pass the CancellationToken to all methods.
35+
string content = await response.Content.ReadAsStringAsync(cancellationSource.Token);
2136
//</SnippetHttpCompletionOption>
2237
}
2338
}

xml/System.Net.Http/HttpCompletionOption.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
<format type="text/markdown"><![CDATA[
4141
4242
## Remarks
43-
The <xref:System.Net.Http.HttpCompletionOption> value effectively affects the scope of the timeout specified in the <xref:System.Net.Http.HttpClient> operation options when reading a response. The timeout on the <xref:System.Net.Http.HttpClient> always applies on the relevant invoked methods up until the point where those methods complete/return. Crucially, when using the <xref:System.Net.Http.HttpCompletionOption.ResponseHeadersRead> option, the timeout applies only up to where the headers end and the content starts. The content reading operation needs to be timed out separately in case the server promptly returns the status line and headers but takes too long to return the content. Below is an example illustrating this point:
43+
44+
> [!WARNING]
45+
> The <xref:System.Net.Http.HttpCompletionOption> value affects the scope of the timeout specified in the <xref:System.Net.Http.HttpClient> options when reading a response. The timeout on the <xref:System.Net.Http.HttpClient> always applies on the relevant invoked methods up until the point where those methods complete/return. Crucially, when using the <xref:System.Net.Http.HttpCompletionOption.ResponseHeadersRead> option, the timeout applies only up to where the headers end and the content starts. The content reading operation needs to be timed out separately in case the server promptly returns the status line and headers but takes too long to return the content.
46+
> This consideration also applies to the <xref:System.Net.Http.HttpClient.MaxResponseContentBufferSize%2A> property. The limit is only enforced when using <xref:System.Net.Http.HttpCompletionOption.ResponseContentRead>.
47+
> Below is an example illustrating this point:
4448
4549
:::code language="csharp" source="~/snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs" id="SnippetHttpCompletionOption":::
4650

0 commit comments

Comments
 (0)