File tree Expand file tree Collapse file tree 1 file changed +3
-1
lines changed
docs/fundamentals/networking/snippets/httpclient Expand file tree Collapse file tree 1 file changed +3
-1
lines changed Original file line number Diff line number Diff line change 3
3
static async Task WithCancellationAndInnerTimeoutAsync ( HttpClient httpClient )
4
4
{
5
5
// <innertimeout>
6
+ using var cts = new CancellationTokenSource ( ) ;
6
7
try
7
8
{
8
9
// Assuming:
9
10
// httpClient.Timeout = TimeSpan.FromSeconds(10)
10
11
11
12
using var response = await httpClient . GetAsync (
12
- "http://localhost:5001/sleepFor?seconds=100" ) ;
13
+ "http://localhost:5001/sleepFor?seconds=100" , cts . Token ) ;
13
14
}
14
15
catch ( OperationCanceledException ex ) when ( ex . InnerException is TimeoutException tex )
15
16
{
17
+ // when the time-out occurred. Here the cancellation token has not been canceled.
16
18
Console . WriteLine ( $ "Timed out: { ex . Message } , { tex . Message } ") ;
17
19
}
18
20
// </innertimeout>
You can’t perform that action at this time.
0 commit comments