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

Handles faults by executing an alternative operation when the original one is failing, also provides the ability to produce an alternative result value when the original operation is not able to do it.

Configuration

  • For operations without a return value
    // Create a new bot policy
    var policy = new BotPolicy();
    
    // Configure the policy to use fallback
    policy.Configure(policyConfig => policyConfig
        .Fallback(fallbackConfig => fallbackConfig
    
            // Set a delegate to determine whether the given fallback 
            // operation should be executed or not when a specific exception occurs.
            .WhenExceptionOccurs(exception => exception is HttpRequestException)
    
            // Set a delegate to invoke when the original operation is failed.
            .OnFallback((exception, context) => DoFallbackOperation())));
  • For operations with a return value
    // Create a new bot policy
    var policy = new BotPolicy<HttpResponseMessage>();
    
    // Configure the policy to use fallback
    policy.Configure(policyConfig => policyConfig
        .Fallback(fallbackConfig => fallbackConfig
    
            // Set a delegate to determine whether the given fallback 
            // operation should be executed or not based on the originals return value.
            .WhenResultIs(result => result.StatusCode != HttpStatusCode.Ok)
    
            // Set a delegate to invoke asynchronously when the original operation is failed. 
            // It must provide an alternative return value.
            .OnFallback((result, exception, context) => DoFallbackOperation())));

Available configuration options

  • Same for policies with or without a return value

    • .WhenExceptionOccurs(Func<Exception, bool>) - Sets the delegate to determine whether the given fallback operation should be executed or not when a specific exception occurs.
  • Only for policies without a return value

    • .OnFallback(Action<Exception, ExecutionContext>) - Sets the delegate to invoke when the original operation is failed.
    • .OnFallbackAsync(Func<Exception, AttemptContext, CancellationToken, Task>) - Sets the delegate to invoke asynchronously when the original operation is failed.
  • Only for policies with a return value

    • .WhenResultIs(Func<TResult, bool>) - Sets the delegate to determine whether the given operation should be re-executed or not based on its return value.
    • .OnFallback(Func<TResult, Exception, ExecutionContext, TResult>) - Sets the delegate to invoke when the original operation is failed. Also provides an alternative return value.
    • .OnFallbackAsync(Func<TResult, Exception, ExecutionContext, CancellationToken, Task<TResult>>) - Sets the delegate to invoke asynchronously when the original operation is failed. Also provides an alternative return value.
Clone this wiki locally