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 ofResilienceContext 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
TheResilienceContext 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 withResilienceContext:
1
Define resilience property keys
Create strongly-typed keys for your custom data:
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 newResilienceContext 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 severalGet methods to initialize properties:
Common Use Cases
Tracking correlation IDs
Tracking correlation IDs
Use context to pass correlation IDs through your pipeline:
Passing request metadata
Passing request metadata
Share request-specific metadata across retry attempts:
Collecting retry metrics
Collecting retry metrics
Track retry attempts and timing:
Operation Key and Telemetry
TheOperationKey property is particularly important for telemetry:
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 someExecute 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