Exemplo n.º 1
0
        /// <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));
        }
Exemplo n.º 2
0
        /// <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));
        }