A retry policy improves resilience when a remote HTTP request fails temporarily. It repeats the request under configured conditions and limits, then returns the error if attempts are exhausted. section 33.0 section 33.3
Configure a retry policy for a named or typed HttpClient:
- Install Microsoft.Extensions.Http.Polly. section 33.3
- Register the client with IHttpClientFactory. section 33.3
- Add transient-error handling with AddTransientHttpErrorPolicy(). section 33.3
- Choose the retry count and waiting intervals, then let the handler return a successful response or the final error. section 33.3
Retry an exchange-rates request
Register ExchangeRatesClient with AddHttpClient<ExchangeRatesClient>().AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1) })); The handler retries three times with increasing delays. By default, it handles HttpRequestException, HTTP 5xx responses, and HTTP 408 timeouts. section 33.3
Figure 33.5 Using the PolicyHttpMessageHandler to handle transient errors. If an error occurs when calling the remote API, the Polly handler will automatically retry the request. If the request then succeeds, the result is passed back to the caller. The caller didn’t have to handle the error, making it simpler to use the HttpClient while remaining resilient to transient errors.
Visualizes the PolicyHttpMessageHandler retrying a failed remote request and passing the successful result back to the caller. section 33.3
Retry only requests that are safe to repeat. A request that will never succeed may be better handled by failing quickly; circuit breakers and other Polly strategies provide alternatives for more advanced resilience. section 33.5




















