Skip to main content
The Polly.Extensions package provides integration with Microsoft.Extensions.DependencyInjection, enabling you to register and manage resilience pipelines through the service collection.

Installation

Core Extension Methods

AddResiliencePipeline

Registers a resilience pipeline with the service collection.
IServiceCollection
required
The service collection to add the resilience pipeline to
TKey
required
The key used to identify the resilience pipeline
Action<ResiliencePipelineBuilder>
required
An action that configures the resilience pipeline
Returns: The updated IServiceCollection with the registered resilience pipeline. Example:

AddResiliencePipeline (with context)

Registers a resilience pipeline with access to the service provider and pipeline context.
IServiceCollection
required
The service collection to add the resilience pipeline to
TKey
required
The key used to identify the resilience pipeline
Action<ResiliencePipelineBuilder, AddResiliencePipelineContext<TKey>>
required
An action that configures the resilience pipeline with access to context
Example:

AddResiliencePipeline<TResult>

Registers a resilience pipeline that handles a specific result type.
IServiceCollection
required
The service collection to add the resilience pipeline to
TKey
required
The key used to identify the resilience pipeline
Action<ResiliencePipelineBuilder<TResult>>
required
An action that configures the resilience pipeline for the specific result type
Example:

AddResiliencePipelines

Allows deferred addition of one or more resilience pipelines.
IServiceCollection
required
The service collection to add resilience pipelines to
Action<AddResiliencePipelinesContext<TKey>>
required
An action that allows configuration of multiple resilience pipelines
Example:

AddResiliencePipelineRegistry

Registers ResiliencePipelineRegistry<TKey> and ResiliencePipelineProvider<TKey> to the service collection.
IServiceCollection
required
The service collection to add the registry to
Example:

Context Classes

AddResiliencePipelineContext<TKey>

Represents the context for adding a resilience pipeline with the specified key.

Properties

Methods

EnableReloads<TOptions> Enables dynamic reloading of the resilience pipeline whenever the specified options are changed.
string?
The named options, if any. If null, global options are listened to
GetOptions<TOptions> Gets the options identified by name.
string?
The options name, if any. If null, global options are returned
OnPipelineDisposed Registers a callback that is called when the pipeline instance is disposed.
Action
required
The callback delegate to invoke on disposal

AddResiliencePipelinesContext<TKey>

Represents the context for configuring multiple resilience pipelines.

Properties

Methods

AddResiliencePipeline Adds a resilience pipeline to the registry.

ResiliencePipelineProvider<TKey>

Provides access to resilience pipelines that are registered by key.

Methods

GetPipeline Retrieves a resilience pipeline using the specified key.
TKey
required
The key used to identify the resilience pipeline
Returns: The resilience pipeline associated with the specified key. Throws: KeyNotFoundException when no resilience pipeline is found for the specified key. GetPipeline<TResult> Retrieves a generic resilience pipeline using the specified key.
TKey
required
The key used to identify the resilience pipeline
Returns: The generic resilience pipeline associated with the specified key. TryGetPipeline Tries to get a resilience pipeline using the specified key.
Returns: true if the pipeline was found, false otherwise.

Telemetry Options

TelemetryOptions

Configures the telemetry that is produced by resilience strategies.
Example:

Usage Example

Complete example of registering and using resilience pipelines:

See Also