Skip to content

Commit 5777abc

Browse files
Update the code snippet of 'Use HTTP error handling' in httpclient.md / No.1 (#48218)
1 parent d3870c3 commit 5777abc

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

docs/fundamentals/networking/snippets/httpclient/Program.CancellationInnerTimeout.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
static async Task WithCancellationAndInnerTimeoutAsync(HttpClient httpClient)
44
{
55
// <innertimeout>
6+
using var cts = new CancellationTokenSource();
67
try
78
{
89
// Assuming:
910
// httpClient.Timeout = TimeSpan.FromSeconds(10)
1011

1112
using var response = await httpClient.GetAsync(
12-
"http://localhost:5001/sleepFor?seconds=100");
13+
"http://localhost:5001/sleepFor?seconds=100", cts.Token);
1314
}
1415
catch (OperationCanceledException ex) when (ex.InnerException is TimeoutException tex)
1516
{
17+
// when the time-out occurred. Here the cancellation token has not been canceled.
1618
Console.WriteLine($"Timed out: {ex.Message}, {tex.Message}");
1719
}
1820
// </innertimeout>

0 commit comments

Comments
 (0)