Skip to main content

Overview

The ResiliencePipelineBuilder class is used to create instances of ResiliencePipeline by combining multiple resilience strategies. Strategies are executed in the order they were added to the builder.

Classes

ResiliencePipelineBuilder

Builder for creating non-generic ResiliencePipeline instances.

ResiliencePipelineBuilder<TResult>

Builder for creating generic ResiliencePipeline<TResult> instances that handle specific result types.
Type
The type of result the pipeline will handle.

Properties

Name

string?
The name of the builder. This property is included in telemetry produced by individual resilience strategies.Default value: null

InstanceName

string?
The instance name of the builder. Used to differentiate between multiple builder instances with the same Name in telemetry.Default value: null

TimeProvider

TimeProvider?
A TimeProvider that is used by strategies that work with time (e.g., retry delays, timeouts).Default value: null (uses TimeProvider.System)

ContextPool

ResilienceContextPool?
The ResilienceContextPool associated with the builder. A custom pool can be used to configure custom behavior for context creation.Default value: null (uses ResilienceContextPool.Shared)

TelemetryListener

TelemetryListener?
The TelemetryListener that is used by Polly to report resilience events. This property is used by the telemetry infrastructure and should not be used directly by user code.Default value: null

Methods

Build

Builds the resilience pipeline with all configured strategies.
ResiliencePipeline
An instance of ResiliencePipeline or ResiliencePipeline<TResult>.
ValidationException
Thrown when the builder has invalid configuration.
The builder cannot be reused after calling Build(). Attempting to add more strategies will throw an InvalidOperationException.

Extension Methods

AddPipeline

Adds an already created pipeline instance to the builder.
TBuilder
required
The builder instance.
ResiliencePipeline
required
The pipeline instance to add.
TBuilder
The same builder instance for chaining.
ArgumentNullException
Thrown when pipeline is null.
InvalidOperationException
Thrown when the builder was already used to create a pipeline.

AddStrategy

Adds a resilience strategy to the builder.
TBuilder
required
The builder instance.
Func<StrategyBuilderContext, ResilienceStrategy>
required
The factory that creates a resilience strategy.
ResilienceStrategyOptions
required
The options associated with the strategy.
TBuilder
The same builder instance for chaining.
ArgumentNullException
Thrown when builder, factory, or options is null.
InvalidOperationException
Thrown when the builder was already used to create a pipeline.
ValidationException
Thrown when options is invalid.

AddStrategy (without options)

Adds a resilience strategy to the builder without explicit options.
TBuilder
required
The builder instance.
Func<StrategyBuilderContext, ResilienceStrategy>
required
The factory that creates a resilience strategy.
TBuilder
The same builder instance for chaining.

Example Usage

Basic Pipeline

Named Pipeline with Telemetry

Generic Pipeline for HTTP Responses

Composing Multiple Pipelines

Custom Strategy

Using Custom TimeProvider for Testing

Strategy Execution Order

Strategies are executed in the order they are added to the builder. The first strategy added wraps all subsequent strategies.
In this example:
  1. Retry wraps everything - if the circuit breaker or timeout fails, retry can attempt again
  2. Circuit breaker wraps timeout - protects against repeated timeout failures
  3. Timeout wraps the actual operation - ensures individual attempts don’t take too long
The order of strategies matters significantly. Generally, you want retry as the outermost layer, followed by circuit breaker, then timeout or rate limiter for the innermost layers.