Introduction
Polly can be extended with custom resilience strategies to meet your specific needs. This guide explains how to create both reactive and proactive resilience strategies.Strategy Types
Polly identifies two types of resilience strategies:Reactive Strategies
Handle specific exceptions or results returned by callbacks
Proactive Strategies
Make proactive decisions to cancel or reject callback execution
Core Components
Every resilience strategy requires these components:Strategy Implementation
The strategy class that inherits from
ResilienceStrategy or ResilienceStrategy<T>Component Architecture
The diagram below shows how custom components interact with Polly’s built-in types:Delegates
Resilience strategies use three types of delegates:Predicates
Determine whether a strategy should handle a given execution result.Events
Triggered when significant actions or states occur.- Reactive:
Func<Args<TResult>, ValueTask> - Proactive:
Func<Args, ValueTask>
Generators
Invoked when the strategy needs specific values from the caller.- Reactive:
Func<Args<TResult>, ValueTask<TValue>> - Proactive:
Func<Args, ValueTask<TValue>>
All delegates are asynchronous and return a
ValueTask. When setting up delegates, consider using ResilienceContext.ContinueOnCapturedContext if your code interacts with a synchronization context.Delegate Usage Examples
Arguments
Arguments flow information from the strategy to delegate consumers. They should:- Always have an
Argumentssuffix - Include a
Contextproperty - Be implemented as readonly structs
Example: Proactive Arguments
Next Steps
Custom Reactive Strategy
Create a strategy that handles specific results or exceptions
Custom Proactive Strategy
Build a strategy that makes proactive execution decisions