Esempio n. 1
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will wait and retry <paramref name="retryCount" /> times
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception and the current sleep duration.
        ///     On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider" /> with
        ///     the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be equal to or greater than zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        ///     timeSpanProvider
        ///     or
        ///     onRetry
        /// </exception>
        public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount,
                                               Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            IEnumerable <TimeSpan> sleepDurations = Enumerable.Range(1, retryCount)
                                                    .Select(sleepDurationProvider);

            return(new Policy(
                       (action, continueOnCapturedContext) => RetryPolicy.ImplementationAsync(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleep(sleepDurations, onRetry),
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Esempio n. 2
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry indefinitely
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, Action <Exception> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       action => RetryPolicy.ImplementationAsync(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyState(onRetry))
                       ));
        }
Esempio n. 3
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry indefinitely
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy RetryForeverAsync(this PolicyBuilder policyBuilder, Action <Exception> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       (action, cancellationToken, continueOnCapturedContext) => RetryPolicy.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyState(onRetry),
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Esempio n. 4
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry <paramref name="retryCount" /> times
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception and retry count.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int> onRetry)
        {
            if (retryCount <= 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       action => RetryPolicy.ImplementationAsync(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithCount(retryCount, onRetry))
                       ));
        }
Esempio n. 5
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will wait and retry as many times as there are provided
        ///     <paramref name="sleepDurations" />
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception and the current sleep duration.
        ///     On each retry, the duration to wait is the current <paramref name="sleepDurations" /> item.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">
        ///     sleepDurations
        ///     or
        ///     onRetry
        /// </exception>
        public static Policy WaitAndRetryAsync(this PolicyBuilder policyBuilder, IEnumerable <TimeSpan> sleepDurations,
                                               Action <Exception, TimeSpan> onRetry)
        {
            if (sleepDurations == null)
            {
                throw new ArgumentNullException("sleepDurations");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       action => RetryPolicy.ImplementationAsync(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleep(sleepDurations, onRetry))
                       ));
        }
Esempio n. 6
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry <paramref name="retryCount" /> times
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception and retry count.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be equal to or greater than zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be equal to or greater than zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       (action, cancellationToken, continueOnCapturedContext) => RetryPolicy.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithCount(retryCount, onRetry),
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }