Skip to content

Commit f113ee0

Browse files
foviedoITBAFERNAN OVIEDO CANDELARESIMihaZupan
authored
Add remark about timeout scope on HttpCompletionOptions enum (#10750)
* Add remark about timeout scope on HttpCompletionOptions enum * Add EnsureStatusCode invokation to illustrate the point of the ResponseHeadersRead option * Clarify the scenario * Replace 'see' tags with 'xref' * Move code sample to standalone snippet * Fully qualify HttpCompletionOption xref in markdown * Apply suggestions from code review about MaxResponseContentBufferSize and rewording Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Bump sample to .NET 9 (using new LoadIntoBufferAsync overload) --------- Co-authored-by: FERNAN OVIEDO CANDELARESI <feoviedo@microsoft.com> Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
1 parent 2f05d89 commit f113ee0

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
5+
namespace HttpCompletionOptionSnippets
6+
{
7+
class HttpCompletionOptionSnippets
8+
{
9+
public static async Task RunAsync()
10+
{
11+
//<SnippetHttpCompletionOption>
12+
using var httpClient = new HttpClient();
13+
httpClient.Timeout = TimeSpan.FromSeconds(30);
14+
httpClient.MaxResponseContentBufferSize = 1_000; // This will be ignored
15+
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);
36+
//</SnippetHttpCompletionOption>
37+
}
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace HttpCompletionOptionSnippets
4+
{
5+
class MetadataReaderSnippets
6+
{
7+
class Program
8+
{
9+
static async Task Main()
10+
{
11+
await HttpCompletionOptionSnippets.RunAsync();
12+
}
13+
}
14+
}
15+
}

xml/System.Net.Http/HttpCompletionOption.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,21 @@
3636
</Base>
3737
<Docs>
3838
<summary>Indicates if <see cref="T:System.Net.Http.HttpClient" /> operations should be considered completed either as soon as a response is available, or after reading the entire response message including the content.</summary>
39-
<remarks>To be added.</remarks>
39+
<remarks>
40+
<format type="text/markdown"><![CDATA[
41+
42+
## Remarks
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:
48+
49+
:::code language="csharp" source="~/snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs" id="SnippetHttpCompletionOption":::
50+
51+
]]>
52+
</format>
53+
</remarks>
4054
</Docs>
4155
<Members>
4256
<Member MemberName="ResponseContentRead">

0 commit comments

Comments
 (0)