Overview
TheResiliencePipelineBuilder 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-genericResiliencePipeline instances.
ResiliencePipelineBuilder<TResult>
Builder for creating genericResiliencePipeline<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:
nullInstanceName
string?
The instance name of the builder. Used to differentiate between multiple builder instances with the same
Name in telemetry.Default value: nullTimeProvider
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: nullMethods
Build
ResiliencePipeline
An instance of
ResiliencePipeline or ResiliencePipeline<TResult>.ValidationException
Thrown when the builder has invalid configuration.
Extension Methods
AddPipeline
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
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)
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.- Retry wraps everything - if the circuit breaker or timeout fails, retry can attempt again
- Circuit breaker wraps timeout - protects against repeated timeout failures
- Timeout wraps the actual operation - ensures individual attempts don’t take too long