Skip to main content
The ResilienceContext class provides an execution-scoped instance that accompanies each execution through a Polly resilience pipeline. It enables you to share context and facilitate information exchange between different stages of execution and across multiple strategies.

What is Resilience Context?

Think of ResilienceContext as a backpack that travels with your request through the entire pipeline. It carries important information that strategies and your code can access and modify along the way.
The resilience context is shared across all strategies in a pipeline and persists throughout the entire execution, including across retry attempts.

Context Properties

The ResilienceContext exposes several key properties:

OperationKey

A user-defined identifier for the operation, useful for telemetry and logging.

CancellationToken

The cancellation token associated with the operation.

Properties

A collection of custom key-value pairs for attaching data to the context.

ContinueOnCapturedContext

Controls whether async execution continues on the captured synchronization context.

Basic Usage

Here’s how to work with ResilienceContext:
1

Define resilience property keys

Create strongly-typed keys for your custom data:
Define keys in a static class for easy discovery and maintenance. The ResiliencePropertyKey<T> is a lightweight struct-based API.
2

Get a context from the pool

Acquire a context instance from the shared pool:
3

Attach custom data

Add your custom data to the context:
4

Use the context in your pipeline

Pass the context when executing the pipeline:
5

Return the context to the pool

Always return the context to the pool when done:

Complete Example

Here’s a complete example showing context usage:

Context Flow Through Pipeline

The context flows through all stages of pipeline execution:
The same context instance flows through all retry attempts, allowing you to track state across attempts.

Context Pooling

Creating new ResilienceContext instances for each execution would be expensive. Polly provides ResilienceContextPool to reuse instances:

Why Use Pooling?

Reduced Allocations

Reusing context instances significantly reduces memory allocations and garbage collection pressure.

Better Performance

Pooling eliminates the overhead of creating and destroying context objects for each execution.

Pool Methods

The pool provides several Get methods to initialize properties:
Always return contexts to the pool when done. However, it’s safe to skip returning in exception scenarios if you want to avoid try-catch blocks.

Common Use Cases

Use context to pass correlation IDs through your pipeline:
Share request-specific metadata across retry attempts:
Track retry attempts and timing:
Pass information from one strategy to another:

Operation Key and Telemetry

The OperationKey property is particularly important for telemetry:
Operation keys are reported in telemetry metrics. Avoid using unbounded values (like user IDs or GUIDs) as operation keys, as this can lead to metric cardinality explosion.

Good Operation Keys

Bad Operation Keys

Best Practices

1

Always use the context pool

2

Define keys in a central location

3

Use typed keys

4

Return contexts in finally blocks

Context vs State Parameter

You might have noticed that some Execute methods accept both a context and a state parameter:

State Parameter

When to use: Pass parameters to your callback without closures (performance optimization).Scope: Only accessible inside your callback.Purpose: Avoid memory allocations from closures and enable static methods.

Context Parameter

When to use: Share information across strategies and retry attempts.Scope: Accessible throughout the entire pipeline execution.Purpose: Exchange data between strategy delegates and execution attempts.

Example: State vs Context

Next Steps

Resilience Pipelines

Learn how to build and compose resilience pipelines

Resilience Strategies

Explore the available resilience strategies

Telemetry

Understand how operation keys are used in telemetry