Skip to content
Peter Csajtai edited this page Jul 9, 2020 · 5 revisions

Allows configuring auto re-execution of an operation based on exceptions or on its return value.

Configuration

  • For operations without a return value
    // Create a new bot policy
    var policy = new BotPolicy();
    
    // Configure the policy to retry failed operations
    policy.Configure(policyConfig => policyConfig
        .Retry(retryConfig => retryConfig
    
            // Set the maximum retry count
            .WithMaxAttemptCount(5)
    
            // Set the predicate to decide that whether an 
            // exception should be handled or not
            .WhenExceptionOccurs(exception => exception is HttpRequestException)
    
            // Set the delegate function used to calculate the amount of time to 
            // wait between the retry attempts
            .WaitBetweenAttempts((attempt, exception) => TimeSpan.FromSeconds(5))
    
            // (optional) Set a callback delegate to invoke when the 
            // given operation is being re-executed
            .OnRetry((exception, attemptContext) => Console.WriteLine($"{attemptContext.CurrentAttempt}. retry attempt, waiting {attemptContext.CurrentDelay}"))));
  • For operations with a return value
    // Create a new bot policy
    var policy = new BotPolicy<HttpResponseMessage>();
    
    // Configure the policy to retry failed operations
    policy.Configure(policyConfig => policyConfig
        .Retry(retryConfig => retryConfig
    
            // Set the maximum retry count
            .WithMaxAttemptCount(5)
    
            // Additionally you can set a predicate to decide
            // whether the result of the operation should be handled or not
            .WhenResultIs(result => result.StatusCode != HttpStatusCode.Ok)
    
            // Set the delegate function used to calculate the amount of time to wait 
            // between the retry attempts
            .WaitBetweenAttempts((attempt, result, exception) => TimeSpan.FromSeconds(5))
    
            // (optional) Set a callback delegate to invoke when the 
            // given operation is being re-executed
            .OnRetry((exception, result, attemptContext) => Console.WriteLine($"{attemptContext.CurrentAttempt}. retry attempt, waiting {attemptContext.CurrentDelay}, result was: {result}"))));

Available configuration options

  • Same for policies with or without a return value

    • .WithMaxAttemptCount(int) - Sets the maximum number of retry attempts.
    • .RetryIndefinitely() - Sets the maximum number of retry attempts to int.MaxValue.
    • .WaitBetweenAttempts(Func<int, Exception, TimeSpan>) - Sets the delegate to calculate the wait time between the retry attempts.
    • .WhenExceptionOccurs(Func<Exception, bool>) - Sets the delegate to determine whether the given operation should be re-executed or not when a specific exception occurs.
    • .OnRetry(Action<Exception, AttemptContext>) - Sets the delegate to invoke when the given operation is being re-executed.
    • .OnRetryAsync(Func<Exception, AttemptContext, CancellationToken, Task>) - Sets the delegate which will be invoked asynchronously when the given operation is being re-executed.
    • .OnRetrySucceeded(Action<AttemptContext>) - Sets the delegate to invoke when the given operation is healed from re-execution.
    • .OnRetrySucceededAsync(Func<AttemptContext, CancellationToken, Task>) - Sets the delegate which will be invoked asynchronously when the given operation is healed from re-execution.
    • .OnRetryLimitReached(Action<Exception, ExecutionContext>) - Sets the delegate to invoke when the given maximum retry count reached.
    • .OnRetryLimitReachedAsync(Func<Exception, ExecutionContext, CancellationToken, Task>) - Sets the delegate to invoke asynchronously when the given maximum retry count reached.
  • Only for policies with a return value

    • .WaitBetweenAttempts(Func<int, Exception, TResult, TimeSpan>) - Sets the delegate to calculate the wait time between the retry attempts.
    • .WhenResultIs(Func<TResult, bool>) - Sets the delegate to determine whether the given operation should be re-executed or not based on its return value.
    • .OnRetry(Action<TResult, Exception, AttemptContext>) - Sets the delegate to invoke when the given operation is being re-executed.
    • .OnRetryAsync(Func<TResult, Exception, AttemptContext, CancellationToken, Task>) - Sets the delegate to invoke asynchronously when the given operation is being re-executed.
    • .OnRetrySucceeded(Action<TResult, AttemptContext>) - Sets the delegate to invoke when the given operation is healed from re-execution.
    • .OnRetrySucceededAsync(Func<TResult, AttemptContext, CancellationToken, Task>) - Sets the delegate to invoke asynchronously when the given operation is healed from re-execution.

When the number of re-executions are reaching the configured maximum attempt count, the framework throws a custom MaxRetryAttemptsReachedException exception.

Clone this wiki locally