Skip to content

Commit 1a44c31

Browse files
author
FERNAN OVIEDO CANDELARESI
committed
Move code sample to standalone snippet
1 parent 87b4042 commit 1a44c31

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
var httpClient = new HttpClient();
13+
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();
18+
19+
var content = await response.Content.ReadAsStringAsync(); // NO TIMEOUT
20+
}
21+
//</SnippetHttpCompletionOption>
22+
}
23+
}
24+
}
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>net8.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: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,7 @@
4242
## Remarks
4343
The <xref: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:
4444
45-
```csharp
46-
var httpClient = new HttpClient();
47-
httpClient.Timeout = TimeSpan.FromSeconds(30);
48-
using (var response = await httpClient.GetAsync("http://localhost:12345/", HttpCompletionOption.ResponseHeadersRead)) // 30-second timeout but ONLY up until past the headers
49-
{
50-
// Do other stuff that doesn't rely on the content first, like status code validation
51-
response.EnsureSuccessStatusCode();
52-
53-
var content = await response.Content.ReadAsStringAsync(); // NO TIMEOUT
54-
}
55-
```
45+
:::code language="csharp" source="~/snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs" id="SnippetHttpCompletionOption":::
5646
5747
]]>
5848
</format>

0 commit comments

Comments
 (0)