liveBook · Manning

Polly

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

Polly is a .NET resilience library for defining policies that handle faults and control retries, timeouts, circuit breakers, and related behaviors.

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

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

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

How deep it goes
Explains package setup, handler-pipeline integration, retry timing, supported errors, and policy trade-offs without detailing Polly internals.
Examples
Uses an ExchangeRatesClient with three delayed retries and a PolicyHttpMessageHandler flow diagram.
What it assumes
Assumes basic HttpClient and remote-API usage while introducing policy registration through IHttpClientFactory.
What you can do after
Operate/configure Polly through AddTransientHttpErrorPolicy() to retry selected transient HTTP failures automatically.
How it connects
Connects Polly with HttpClient, IHttpClientFactory, typed clients, named clients, exponential backoff, circuit breaking, and bulkhead isolation.

Also explained in

Compare how 2 books explain Polly →

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.