0Pricing
C# Academy · Lesson

Resilience Pipelines

Combine strategies into resilience pipelines.

Resilience Pipelines is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Composing Strategies

Real resilience usually needs several strategies working together. Polly v8 lets you compose them into a single resilience pipeline using the ResiliencePipelineBuilder.

The ResiliencePipelineBuilder

You chain Add... calls and finish with Build(). The result is a reusable, thread-safe ResiliencePipeline you execute calls through.

var pipeline = new ResiliencePipelineBuilder()
    .AddRetry(new RetryStrategyOptions())
    .AddTimeout(TimeSpan.FromSeconds(5))
    .Build();

Order Matters

Strategies execute outside-in: the first added wraps the rest. A common, correct ordering is timeout-per-try inside retry, with an overall timeout and circuit breaker around them.

var pipeline = new ResiliencePipelineBuilder()
    .AddTimeout(TimeSpan.FromSeconds(10))      // overall
    .AddRetry(new RetryStrategyOptions())
    .AddCircuitBreaker(new CircuitBreakerStrategyOptions())
    .AddTimeout(TimeSpan.FromSeconds(2))       // per attempt
    .Build();

Typed Pipelines

When strategies inspect the result (like a fallback or result-based retry), use the generic ResiliencePipelineBuilder<T> so the pipeline knows the return type.

var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
    .AddRetry(new HttpRetryStrategyOptions())
    .Build();

Executing With State

To avoid closures allocating, pass state into ExecuteAsync; Polly hands it back to your callback.

await pipeline.ExecuteAsync(
    static async (state, token) => await state.CallAsync(token),
    myService);

The ResiliencePipelineProvider

Instead of building pipelines inline, register named pipelines in DI and resolve them via ResiliencePipelineProvider. This centralizes configuration.

builder.Services.AddResiliencePipeline("db", pipeline =>
{
    pipeline.AddRetry(new RetryStrategyOptions())
            .AddTimeout(TimeSpan.FromSeconds(3));
});

Resolving A Named Pipeline

Inject ResiliencePipelineProvider<string> and fetch your pipeline by key wherever you need it.

public class OrderService
{
    private readonly ResiliencePipeline _pipeline;
    public OrderService(ResiliencePipelineProvider<string> provider)
    {
        _pipeline = provider.GetPipeline("db");
    }
}

Resilience For HttpClient

The AddResilienceHandler extension attaches a pipeline to a typed or named HttpClient, applying it to every outgoing request.

builder.Services.AddHttpClient<CatalogClient>()
    .AddResilienceHandler("catalog", b =>
    {
        b.AddRetry(new HttpRetryStrategyOptions());
        b.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions());
    });

The Standard Resilience Handler

Microsoft ships a batteries-included AddStandardResilienceHandler that bundles sensible retry, circuit breaker, timeout and rate limiting in the recommended order.

builder.Services.AddHttpClient<CatalogClient>()
    .AddStandardResilienceHandler();

Pipelines Are Reusable And Cheap

Build a pipeline once and reuse it for many executions. They are immutable and thread-safe, so sharing them is both safe and efficient.

Observability Hooks

Every strategy exposes callbacks (OnRetry, OnOpened, OnTimeout, OnFallback), and Polly emits telemetry you can wire into logging and metrics for full visibility.

Quick Check

Test resilience pipelines.

Recap

The ResiliencePipelineBuilder composes retry, circuit breaker, timeout and fallback into one reusable pipeline; ordering is outside-in so it matters. Use the generic builder for result-aware strategies, register named pipelines with AddResiliencePipeline and resolve via ResiliencePipelineProvider. For HTTP, AddResilienceHandler or AddStandardResilienceHandler protect every request.

Frequently asked questions

Is the “Resilience Pipelines” lesson free?

Yes — the full text of “Resilience Pipelines” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Resilience Pipelines”?

Combine strategies into resilience pipelines. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Resilience Pipelines” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Retry Policies
  2. Circuit Breaker Pattern
  3. Timeout and Fallback Policies
  4. Resilience Pipelines
← Back to C# Academy