liveBook · Manning

Retry

As explained in ASP.NET Core in Action, 3e

A retry policy is a resilience rule that repeats a failed operation according to configured retry conditions and limits.

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.
CH33_F05_Lock3

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines setup steps, default failure conditions, delayed retry behavior, limits, and safe-request cautions without detailing advanced strategies.
Examples
Uses an ExchangeRatesClient with three waits of 200 milliseconds, 500 milliseconds, and one second.
What it assumes
Assumes familiarity with HttpClient and named or typed IHttpClientFactory clients, while introducing transient-error policy configuration.
What you can do after
Configure transient HTTP retries for named or typed clients using Microsoft.Extensions.Http.Polly and Polly policies.
How it connects
Connects retry policies with IHttpClientFactory, Polly, transient errors, typed clients, and circuit breakers.

Also explained in

Compare how 21 books explain retry →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.