/// <summary> /// Builds a <see cref="FallbackPolicy"/> which provides a fallback action if the main execution fails. Executes the main delegate, but if this throws a handled exception, first calls <paramref name="onFallback"/> with details of the handled exception and the execution context; then calls <paramref name="fallbackAction"/>. /// </summary> /// <param name="policyBuilder">The policy builder.</param> /// <param name="fallbackAction">The fallback action.</param> /// <param name="onFallback">The action to call before invoking the fallback delegate.</param> /// <exception cref="System.ArgumentNullException">fallbackAction</exception> /// <exception cref="System.ArgumentNullException">onFallback</exception> /// <returns>The policy instance.</returns> public static FallbackPolicy Fallback(this PolicyBuilder policyBuilder, Action <Exception, Context, CancellationToken> fallbackAction, Action <Exception, Context> onFallback) { if (fallbackAction == null) { throw new ArgumentNullException(nameof(fallbackAction)); } if (onFallback == null) { throw new ArgumentNullException(nameof(onFallback)); } return(new FallbackPolicy( (action, context, cancellationToken) => FallbackEngine.Implementation( (ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; }, context, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper <EmptyStruct> .EmptyResultPredicates, (outcome, ctx) => onFallback(outcome.Exception, ctx), (outcome, ctx, ct) => { fallbackAction(outcome.Exception, ctx, ct); return EmptyStruct.Instance; }), policyBuilder.ExceptionPredicates)); }
/// <summary> /// Builds a <see cref="FallbackPolicy"/> which provides a fallback value if the main execution fails. Executes the main delegate, but if this throws a handled exception or raises a handled result, first calls <paramref name="onFallback"/> with details of the handled exception or result and the execution context; then calls <paramref name="fallbackAction"/> and returns its result. /// </summary> /// <param name="policyBuilder">The policy builder.</param> /// <param name="fallbackAction">The fallback action.</param> /// <param name="onFallback">The action to call before invoking the fallback delegate.</param> /// <exception cref="System.ArgumentNullException">fallbackAction</exception> /// <exception cref="System.ArgumentNullException">onFallback</exception> /// <returns>The policy instance.</returns> public static FallbackPolicy <TResult> Fallback <TResult>(this PolicyBuilder <TResult> policyBuilder, Func <DelegateResult <TResult>, Context, CancellationToken, TResult> fallbackAction, Action <DelegateResult <TResult>, Context> onFallback) { if (fallbackAction == null) { throw new ArgumentNullException(nameof(fallbackAction)); } if (onFallback == null) { throw new ArgumentNullException(nameof(onFallback)); } return(new FallbackPolicy <TResult>( (action, context, cancellationToken) => FallbackEngine.Implementation <TResult>( action, context, cancellationToken, policyBuilder.ExceptionPredicates, policyBuilder.ResultPredicates, onFallback, fallbackAction), policyBuilder.ExceptionPredicates, policyBuilder.ResultPredicates)); }
/// <summary> /// Builds a <see cref="FallbackPolicy"/> which provides a fallback action if the main execution fails. Executes the main delegate asynchronously, but if this throws a handled exception, first asynchronously calls <paramref name="onFallbackAsync"/> with details of the handled exception and execution context; then asynchronously calls <paramref name="fallbackAction"/>. /// </summary> /// <param name="policyBuilder">The policy builder.</param> /// <param name="fallbackAction">The fallback delegate.</param> /// <param name="onFallbackAsync">The action to call asynchronously before invoking the fallback delegate.</param> /// <exception cref="System.ArgumentNullException">fallbackAction</exception> /// <exception cref="System.ArgumentNullException">onFallbackAsync</exception> /// <returns>The policy instance.</returns> public static FallbackPolicy FallbackAsync(this PolicyBuilder policyBuilder, Func <CancellationToken, Task> fallbackAction, Func <Exception, Context, Task> onFallbackAsync) { if (fallbackAction == null) { throw new ArgumentNullException(nameof(fallbackAction)); } if (onFallbackAsync == null) { throw new ArgumentNullException(nameof(onFallbackAsync)); } return(new FallbackPolicy( (action, context, cancellationToken, continueOnCapturedContext) => FallbackEngine.ImplementationAsync( async(ctx, ct) => { await action(ctx, ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; }, context, policyBuilder.ExceptionPredicates, PredicateHelper <EmptyStruct> .EmptyResultPredicates, (outcome, ctx) => onFallbackAsync(outcome.Exception, ctx), async ct => { await fallbackAction(ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; }, cancellationToken, continueOnCapturedContext), policyBuilder.ExceptionPredicates)); }
/// <summary> /// Builds a <see cref="FallbackPolicy"/> which provides a fallback value if the main execution fails. Executes the main delegate asynchronously, but if this throws a handled exception or raises a handled result, first asynchronously calls <paramref name="onFallbackAsync"/> with details of the handled exception or result and the execution context; then asynchronously calls <paramref name="fallbackAction"/> and returns its result. /// </summary> /// <param name="policyBuilder">The policy builder.</param> /// <param name="fallbackAction">The fallback delegate.</param> /// <param name="onFallbackAsync">The action to call asynchronously before invoking the fallback delegate.</param> /// <exception cref="System.ArgumentNullException">fallbackAction</exception> /// <exception cref="System.ArgumentNullException">onFallbackAsync</exception> /// <returns>The policy instance.</returns> public static FallbackPolicy <TResult> FallbackAsync <TResult>(this PolicyBuilder <TResult> policyBuilder, Func <CancellationToken, Task <TResult> > fallbackAction, Func <DelegateResult <TResult>, Context, Task> onFallbackAsync) { if (fallbackAction == null) { throw new ArgumentNullException(nameof(fallbackAction)); } if (onFallbackAsync == null) { throw new ArgumentNullException(nameof(onFallbackAsync)); } return(new FallbackPolicy <TResult>( (action, context, cancellationToken, continueOnCapturedContext) => FallbackEngine.ImplementationAsync( action, context, policyBuilder.ExceptionPredicates, policyBuilder.ResultPredicates, onFallbackAsync, fallbackAction, cancellationToken, continueOnCapturedContext), policyBuilder.ExceptionPredicates, policyBuilder.ResultPredicates)); }