Polly is a .NET resilience library that handles transient HTTP failures without pushing retry logic into every caller. Through the HttpClient handler pipeline, it can retry failed requests and support broader strategies such as exponential backoff, circuit breaking, and bulkhead isolation. section 33.3 section 33.5
Add a retry policy to a named or typed HttpClient in three practical steps:
- Install the Microsoft.Extensions.Http.Polly NuGet package. section 33.3
- Register the client with AddHttpClient<T>() or a named-client equivalent. section 33.3
- Call AddTransientHttpErrorPolicy() and provide a Polly policy, such as WaitAndRetryAsync(). section 33.5
Retrying an ExchangeRatesClient
builder.Services.AddHttpClient<ExchangeRatesClient>() .AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1) })); This policy retries three times with increasing delays. The caller receives the successful result without implementing the retry loop. Default transient handling includes connection errors, HTTP 5xx responses, and 408 timeouts. section 33.3 section 33.5
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.
A PolicyHttpMessageHandler retries a failed remote request inside the handler pipeline, returning the successful response to the caller when a retry works. section 33.3

