コード例 #1
0
        /// <summary>
        /// See common summary for RetryHelper
        /// </summary>
        /// <typeparam name="TResult">The type of return value of <paramref name="func"/></typeparam>
        /// <param name="func">The Func<out TResult> delegate to be executed until success</param>
        /// <param name="result">The return value from the execution of <paramref name="func"/></param>
        /// <param name="preamble"> The delegate to be executed in-between retries. </param>
        /// <param name="ignoredExceptions">List if exception types against which resilience is desired</param>
        /// <param name="retries">Number of times to try executing the given method before giving up. Default is 3</param>
        /// <param name="throwOnFailure">Indicates whether the method should throw RetriesExhaustedException, or not</param>
        /// <returns>
        /// True upon successful execution of the method, otherwise False. If <paramref name="throwOnFailure"/> is True, then the method will never return False
        /// </returns>
        internal static bool TryExecuteFunction <TResult>(
            Func <TResult> func,
            out TResult result,
            RetryPreamble preamble,
            List <Type> ignoredExceptions,
            int retries         = 3,
            bool throwOnFailure = false)
        {
            ValidateExceptionTypeList(ignoredExceptions);

            result = default(TResult);

            int  retryCount = retries;
            bool success    = false;


            bool retryPreambleSucceeded = true;

            do
            {
                try
                {
                    if (func != null)
                    {
                        result = func.Invoke();
                    }

                    success = true;
                    break;
                }
                catch (Exception e) when(MatchException(e, ignoredExceptions))
                {
                }

                retryCount--;
                if (retryCount > 0)
                {
                    retryPreambleSucceeded = preamble();
                }
            }while ((retryCount > 0) && retryPreambleSucceeded);

            if (!success && throwOnFailure)
            {
                throw new RetriesExhaustedException();
            }

            return(success);
        }
コード例 #2
0
        /// <summary>
        /// See common summary for RetryHelper
        /// </summary>
        /// <param name="action">The Action delegate to be executed until success</param>
        /// <param name="preamble">
        /// The delegate to be executed in-between retries.
        /// </param>
        /// <param name="ignoredExceptions">List of exception types against which resilience is desired</param>
        /// <param name="retries">Number of times to try executing the given method before giving up. Default is 3</param>
        /// <param name="throwOnFailure">Indicates whether the method should throw RetriesExhaustedException, or not</param>
        /// <returns>
        /// True upon successful execution of the method, otherwise False. If <paramref name="throwOnFailure"/> is True, then the method will never return False
        /// </returns>
        internal static bool TryCallAction(
            Action action,
            RetryPreamble preamble,
            List <Type> ignoredExceptions,
            int retries         = 3,
            bool throwOnFailure = false)
        {
            ValidateExceptionTypeList(ignoredExceptions);

            int  retryCount = retries;
            bool success    = false;

            bool retryPreambleSucceeded = true;

            do
            {
                try
                {
                    action?.Invoke();
                    success = true;
                    break;
                }
                catch (Exception e) when(MatchException(e, ignoredExceptions))
                {
                    // do nothing here
                    // the exception filter does it all
                }

                retryCount--;
                if (retryCount > 0)
                {
                    retryPreambleSucceeded = preamble();
                }
            }while ((retryCount > 0) && retryPreambleSucceeded);

            if (!success && throwOnFailure)
            {
                throw new RetriesExhaustedException();
            }

            return(success);
        }