/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/fundamentals/networking/snippets/httpclient/Program.Cancellation.cs
22 строки
707 B
Genevieve Warren
Change TaskCanceledException to OperationCanceledException (#37364)
04 окт 2023, 18:28
Не верифицирован
04 окт 2023, 18:28
1f283be
Код
Авторство
О чём код?
static partial class Program { static async Task WithCancellationAsync(HttpClient httpClient) { // <cancellation> using var cts = new CancellationTokenSource(); try { // Assuming: // httpClient.Timeout = TimeSpan.FromSeconds(10) using var response = await httpClient.GetAsync( "http://localhost:5001/sleepFor?seconds=100", cts.Token); } catch (OperationCanceledException ex) when (cts.IsCancellationRequested) { // When the token has been canceled, it is not a timeout. Console.WriteLine($"Canceled: {ex.Message}"); } // </cancellation> } }