Beispiel #1
0
        private static T PerformAction <T>(RetryActionWithParametersAndResult <T> action, TimeSpan retryInterval, int retryLimit, RetryPolicy retryPolicy, RetryPolicyOptions retryPolicyOptions, Action <Exception, int, int> onFailure, Func <bool> mustReturnTrueBeforeFail, params Type[] exceptionTypes)
        {
            var exceptions     = new List <Exception>();
            var startTime      = DateTime.Now;
            var retryIteration = 0;

            do
            {
                try
                {
                    // invoke the action
                    return(action(retryIteration, retryLimit));
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                    HandleException(ex, startTime, retryInterval, retryIteration, retryLimit, retryPolicy, retryPolicyOptions, onFailure, exceptionTypes);
                }
                retryIteration++;
            } while (MustContinue(mustReturnTrueBeforeFail) || retryIteration < retryLimit);

            throw new RetryTimeoutException(exceptions, retryLimit);
        }
 /// <summary>
 /// Perform a synchronous retry up to the maximum specified limit
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="action">The Action to call that will be retried until successful.</param>
 /// <param name="retryInterval">How often to perform the retry.</param>
 /// <param name="retryLimit">The maximum number of times to retry</param>
 /// <param name="onFailure">Will be called upon an exception thrown</param>
 /// <param name="retryPolicy">The retry policy to apply</param>
 /// <param name="retryPolicyOptions">The options to provide your retry policy</param>
 /// <param name="mustReturnTrueBeforeFail">Must evaluate to true for retry to fail</param>
 /// <param name="exceptionTypes">A list of exceptions that will be retried gracefully. All other exceptions will be rethrown.</param>
 /// <exception cref="RetryTimeoutException"></exception>
 /// <returns></returns>
 public static T Do <T>(RetryActionWithParametersAndResult <T> action, TimeSpan retryInterval, int retryLimit, RetryPolicy retryPolicy, RetryPolicyOptions retryPolicyOptions, Action <Exception, int, int> onFailure, Func <bool> mustReturnTrueBeforeFail, params Type[] exceptionTypes)
 => PerformAction((x, y) => action.Invoke(x, y), retryInterval, retryLimit, retryPolicy, retryPolicyOptions, onFailure, mustReturnTrueBeforeFail, exceptionTypes);
 /// <summary>
 /// Perform an synchronous retry up to the maximum specified limit
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="action"></param>
 /// <param name="retryInterval">How often to perform the retry.</param>
 /// <param name="retryLimit">The maximum number of times to retry</param>
 /// <param name="retryPolicy">The retry policy to apply</param>
 /// <param name="retryPolicyOptions">Options to specify further configuration for a retry policy</param>
 /// <exception cref="RetryTimeoutException"></exception>
 /// <returns></returns>
 public static T Do <T>(RetryActionWithParametersAndResult <T> action, TimeSpan retryInterval, int retryLimit, RetryPolicy retryPolicy, RetryPolicyOptions retryPolicyOptions)
 => Do(action, retryInterval, retryLimit, retryPolicy, retryPolicyOptions, null, null);
 /// <summary>
 /// Perform an synchronous retry up to the maximum specified limit
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="action"></param>
 /// <param name="retryInterval">How often to perform the retry.</param>
 /// <param name="retryLimit">The maximum number of times to retry</param>
 /// <param name="exceptionTypes">A list of exceptions that will be retried gracefully. All other exceptions will be rethrown.</param>
 /// <exception cref="RetryTimeoutException"></exception>
 /// <returns></returns>
 public static T Do <T>(RetryActionWithParametersAndResult <T> action, TimeSpan retryInterval, int retryLimit, params Type[] exceptionTypes)
 => Do(action, retryInterval, retryLimit, RetryPolicy.StaticDelay, RetryPolicyOptions.None, null, null, exceptionTypes);
 /// <summary>
 /// Perform an synchronous retry up to the maximum specified limit
 /// </summary>
 /// <param name="action"></param>
 /// <param name="retryInterval">How often to perform the retry.</param>
 /// <param name="retryLimit">The maximum number of times to retry</param>
 /// <param name="onFailure">Will be called upon an exception thrown</param>
 /// <exception cref="RetryTimeoutException"></exception>
 public static T Do <T>(RetryActionWithParametersAndResult <T> action, TimeSpan retryInterval, int retryLimit, Action <Exception, int, int> onFailure)
 => Do(action, retryInterval, retryLimit, RetryPolicy.StaticDelay, RetryPolicyOptions.None, onFailure, null);
 /// <summary>
 /// Perform an synchronous retry up to the maximum specified limit
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="action"></param>
 /// <param name="retryLimit">The maximum number of times to retry</param>
 /// <exception cref="RetryTimeoutException"></exception>
 /// <returns></returns>
 public static T Do <T>(RetryActionWithParametersAndResult <T> action, int retryLimit)
 => Do(action, DefaultRetryInterval, retryLimit, RetryPolicy.StaticDelay, RetryPolicyOptions.None, null, null);