Beispiel #1
0
        internal static TResult Implementation <TResult>(
            Func <Context, CancellationToken, TResult> action,
            Context context,
            CancellationToken cancellationToken,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    TResult result = action(context, cancellationToken);

                    if (!shouldRetryResultPredicates.Any(predicate => predicate(result)))
                    {
                        return(result);
                    }

                    if (!policyState.CanRetry(new DelegateResult <TResult>(result), cancellationToken))
                    {
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    Exception handledException = shouldRetryExceptionPredicates
                                                 .Select(predicate => predicate(ex))
                                                 .FirstOrDefault(e => e != null);
                    if (handledException == null)
                    {
                        throw;
                    }

                    if (!policyState.CanRetry(new DelegateResult <TResult>(handledException), cancellationToken))
                    {
                        if (handledException != ex)
                        {
                            ExceptionDispatchInfo.Capture(handledException).Throw();
                        }
                        throw;
                    }
                }
            }
        }
        public static async Task ImplementationAsync(Func <Task> action,
                                                     IEnumerable <ExceptionPredicate> shouldRetryPredicates, Func <IRetryPolicyState> policyStateFactory)
        {
            IRetryPolicyState policyState = policyStateFactory();

            while (true)
            {
                try
                {
                    await action().NotOnCapturedContext();

                    return;
                }
                catch (Exception ex)
                {
                    if (!shouldRetryPredicates.Any(predicate => predicate(ex)))
                    {
                        throw;
                    }

                    if (!policyState.CanRetry(ex))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #3
0
        internal static TResult Implementation <TResult>(
            Func <Context, CancellationToken, TResult> action,
            Context context,
            CancellationToken cancellationToken,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    TResult result = action(context, cancellationToken);

                    if (!shouldRetryResultPredicates.Any(predicate => predicate(result)))
                    {
                        return(result);
                    }

                    if (!policyState.CanRetry(new DelegateResult <TResult>(result), cancellationToken))
                    {
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    if (!shouldRetryExceptionPredicates.Any(predicate => predicate(ex)))
                    {
                        throw;
                    }

                    if (!policyState.CanRetry(new DelegateResult <TResult>(ex), cancellationToken))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #4
0
        internal static TResult Implementation <TResult>(
            Func <TResult> action,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                try
                {
                    DelegateResult <TResult> delegateOutcome = new DelegateResult <TResult>(action());

                    if (!shouldRetryResultPredicates.Any(predicate => predicate(delegateOutcome.Result)))
                    {
                        return(delegateOutcome.Result);
                    }

                    if (!policyState.CanRetry(delegateOutcome))
                    {
                        return(delegateOutcome.Result);
                    }
                }
                catch (Exception ex)
                {
                    if (!shouldRetryExceptionPredicates.Any(predicate => predicate(ex)))
                    {
                        throw;
                    }

                    if (!policyState.CanRetry(new DelegateResult <TResult>(ex)))
                    {
                        throw;
                    }
                }
            }
        }