Overview
Proactive strategies make decisions to cancel or reject callback execution without focusing on individual results. This guide demonstrates how to create a Timing Strategy that tracks execution times and reports when thresholds are exceeded.Proactive strategies inherit from
ResilienceStrategy (non-generic) and work across various result types.Implementation
Strategy Class
Proactive strategies derive from the non-genericResilienceStrategy base class:
Proactive strategies measure or control execution behavior rather than handling specific result types.
Event Arguments
Define arguments to encapsulate event details:Options
1
Define Options Class
Create a public options class that inherits from
ResilienceStrategyOptions:2
Use Validation Attributes
Apply data annotation attributes to validate options:
[Required]for mandatory properties[Range]for value constraints- Pipeline automatically validates during construction
Extension Methods
Proactive strategies can use a single extension method that works for both generic and non-generic builders:By using generic constraints on
ResiliencePipelineBuilderBase, a single extension method supports both ResiliencePipelineBuilder and ResiliencePipelineBuilder<T>.Usage Example
Complete Example
Here’s how the timing strategy works in a complete scenario:1
Create Pipeline
Build a resilience pipeline with the timing strategy:
2
Execute Operations
Use the pipeline to execute operations:
3
Monitor Results
The strategy automatically monitors execution time and triggers events when thresholds are exceeded.
Key Differences from Reactive Strategies
Base Class
Base Class
- Proactive: Inherit from
ResilienceStrategy(non-generic) - Reactive: Inherit from
ResilienceStrategy<T>(generic)
Arguments
Arguments
- Proactive: Arguments only need
Contextproperty - Reactive: Arguments need both
ContextandOutcomeproperties
Focus
Focus
- Proactive: Monitor or control execution behavior
- Reactive: Handle specific results or exceptions
Extension Methods
Extension Methods
- Proactive: Single extension using
ResiliencePipelineBuilderBase - Reactive: Separate extensions for generic and non-generic builders
Key Takeaways
Strategy Implementation
- Inherit from non-generic
ResilienceStrategy - Keep the strategy class internal
- Measure or control execution behavior
- Report events using
ResilienceStrategyTelemetry
Arguments
- Use readonly structs
- Always include
Contextproperty - Add event-specific properties
- Follow naming:
{DelegateName}Arguments
Options
- Inherit from
ResilienceStrategyOptions - Use validation attributes
- Provide sensible defaults
- Set default strategy name
Extension Methods
- Target
ResiliencePipelineBuilderBase - Return builder for fluent API
- Use
AddStrategyfor registration - Automatic options validation
Resources
Timing Strategy Sample
Complete working example from this guide
Timeout Strategy Source
Built-in timeout strategy implementation
Rate Limiter Source
Built-in rate limiter strategy implementation
Extensibility Overview
Learn about extensibility basics