using System.Threading.Tasks;
static HttpClient client = new HttpClient();
static async Task Main(string[] args)
const int timeoutMs = 3000;
const int retryBackOffMs = 1000;
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(timeoutMs), TimeoutStrategy.Optimistic,
(context, timeSpan, task, ex) =>
Console.WriteLine($"Timeout {timeSpan.TotalSeconds} seconds");
return Task.CompletedTask;
Console.WriteLine($"Exception tralal: {ex.Message}");
.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(retryBackOffMs),
(Exception ex, TimeSpan calculatedWaitDuration, int retryCount, Context _) =>
$"Retrying in {calculatedWaitDuration.TotalSeconds} seconds (Reason: {ex.Message}) (Retry count: {retryCount})");
.WrapAsync(timeoutPolicy);
await retryPolicy.ExecuteAsync(async (ct) =>
Console.WriteLine("The request is sent out");
await client.GetAsync("https://httpstat.us/200?sleep=5000", ct);
}, CancellationToken.None);