-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add remark about timeout scope on HttpCompletionOptions enum #10750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MihaZupan
merged 8 commits into
dotnet:main
from
foviedoITBA:HttpCompletionOptionsTimeoutScope
Jan 29, 2025
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcca94f
Add remark about timeout scope on HttpCompletionOptions enum
776ec62
Add EnsureStatusCode invokation to illustrate the point of the Respon…
175f557
Clarify the scenario
87b4042
Replace 'see' tags with 'xref'
1a44c31
Move code sample to standalone snippet
1c8336f
Fully qualify HttpCompletionOption xref in markdown
04c37e4
Apply suggestions from code review about MaxResponseContentBufferSize…
foviedoITBA 95bbed4
Bump sample to .NET 9 (using new LoadIntoBufferAsync overload)
MihaZupan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace HttpCompletionOptionSnippets | ||
{ | ||
class HttpCompletionOptionSnippets | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
//<SnippetHttpCompletionOption> | ||
using var httpClient = new HttpClient(); | ||
httpClient.Timeout = TimeSpan.FromSeconds(30); | ||
httpClient.MaxResponseContentBufferSize = 1_000; // This will be ignored | ||
|
||
// Because we're specifying the ResponseHeadersRead option, | ||
// the 30-second timeout applies only up until the headers are received. | ||
// It does not affect future operations that interact with the response content. | ||
using HttpResponseMessage response = await httpClient.GetAsync( | ||
"http://localhost:12345/", | ||
HttpCompletionOption.ResponseHeadersRead); | ||
|
||
// Do other checks that don't rely on the content first, like status code validation. | ||
response.EnsureSuccessStatusCode(); | ||
|
||
// Since the HttpClient.Timeout will not apply to reading the content, | ||
// you must enforce it yourself, for example by using a CancellationTokenSource. | ||
using var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(15)); | ||
|
||
// If you wish to enforce the MaxResponseContentBufferSize limit as well, | ||
// you can do so by calling the LoadIntoBufferAsync helper first. | ||
await response.Content.LoadIntoBufferAsync(1_000, cancellationSource.Token); | ||
Check failure on line 32 in snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.cs
|
||
|
||
// Make sure to pass the CancellationToken to all methods. | ||
string content = await response.Content.ReadAsStringAsync(cancellationSource.Token); | ||
//</SnippetHttpCompletionOption> | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
snippets/csharp/System.Net.Http/HttpCompletionOption/HttpCompletionOptionSnippets.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
15 changes: 15 additions & 0 deletions
15
snippets/csharp/System.Net.Http/HttpCompletionOption/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace HttpCompletionOptionSnippets | ||
{ | ||
class MetadataReaderSnippets | ||
{ | ||
class Program | ||
{ | ||
static async Task Main() | ||
{ | ||
await HttpCompletionOptionSnippets.RunAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.