Skip to main content

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-generic ResilienceStrategy base class:
Proactive strategies measure or control execution behavior rather than handling specific result types.

Event Arguments

Define arguments to encapsulate event details:
Arguments should always have an Arguments suffix and include a Context property. This design makes the API more extensible and maintainable.

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
Options represent the public contract with consumers. Use validation attributes to ensure correctness.

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

  • Proactive: Inherit from ResilienceStrategy (non-generic)
  • Reactive: Inherit from ResilienceStrategy<T> (generic)
  • Proactive: Arguments only need Context property
  • Reactive: Arguments need both Context and Outcome properties
  • Proactive: Monitor or control execution behavior
  • Reactive: Handle specific results or exceptions
  • 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 Context property
  • 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 AddStrategy for 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