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 #2
0
        public static async Task ImplementationAsync(Func <Task> action, IEnumerable <ExceptionPredicate> shouldRetryPredicates, Func <IRetryPolicyState> policyStateFactory, bool continueOnCapturedContext)
        {
            IRetryPolicyState policyState = policyStateFactory();

            while (true)
            {
                try
                {
                    await action().ConfigureAwait(continueOnCapturedContext);

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

                    if (!(await policyState
                          .CanRetryAsync(ex, continueOnCapturedContext)
                          .ConfigureAwait(continueOnCapturedContext)))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #3
0
        internal static async Task <TResult> ImplementationAsync <TResult>(
            Func <CancellationToken, Task <TResult> > action,
            CancellationToken cancellationToken,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory,
            bool continueOnCapturedContext)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    DelegateResult <TResult> delegateOutcome = new DelegateResult <TResult>(await action(cancellationToken).ConfigureAwait(continueOnCapturedContext));

                    //cancellationToken.ThrowIfCancellationRequested();

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

                    //cancellationToken.ThrowIfCancellationRequested();

                    if (!await policyState
                        .CanRetryAsync(delegateOutcome, cancellationToken, continueOnCapturedContext)
                        .ConfigureAwait(continueOnCapturedContext))
                    {
                        return(delegateOutcome.Result);
                    }
                }
                catch (Exception ex)
                {
                    //if (cancellationToken.IsCancellationRequested)
                    //{
                    //    if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken)
                    //    {
                    //        throw;
                    //    }
                    //    cancellationToken.ThrowIfCancellationRequested();
                    //}

                    if (!shouldRetryExceptionPredicates.Any(predicate => predicate(ex)))
                    {
                        throw;
                    }

                    if (!await policyState
                        .CanRetryAsync(new DelegateResult <TResult>(ex), cancellationToken, continueOnCapturedContext)
                        .ConfigureAwait(continueOnCapturedContext))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #4
0
        internal static async Task <TResult> ImplementationAsync <TResult>(
            Func <Context, CancellationToken, Task <TResult> > action,
            Context context,
            CancellationToken cancellationToken,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory,
            bool continueOnCapturedContext)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext);

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

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

                    if (!await policyState
                        .CanRetryAsync(new DelegateResult <TResult>(handledException), cancellationToken, continueOnCapturedContext)
                        .ConfigureAwait(continueOnCapturedContext))
                    {
                        if (handledException != ex)
                        {
                            ExceptionDispatchInfo.Capture(handledException).Throw();
                        }
                        throw;
                    }
                }
            }
        }
Beispiel #5
0
        internal static async Task <TResult> ImplementationAsync <TResult>(
            Func <CancellationToken, Task <TResult> > action,
            CancellationToken cancellationToken,
            IEnumerable <ExceptionPredicate> shouldRetryExceptionPredicates,
            IEnumerable <ResultPredicate <TResult> > shouldRetryResultPredicates,
            Func <IRetryPolicyState <TResult> > policyStateFactory,
            bool continueOnCapturedContext)
        {
            IRetryPolicyState <TResult> policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    TResult result = await action(cancellationToken).ConfigureAwait(continueOnCapturedContext);

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

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

                    if (!await policyState
                        .CanRetryAsync(new DelegateResult <TResult>(ex), cancellationToken, continueOnCapturedContext)
                        .ConfigureAwait(continueOnCapturedContext))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #6
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 #7
0
        public static async Task ImplementationAsync(Func <CancellationToken, Task> action, CancellationToken cancellationToken, IEnumerable <ExceptionPredicate> shouldRetryPredicates, Func <IRetryPolicyState> policyStateFactory, bool continueOnCapturedContext)
        {
            IRetryPolicyState policyState = policyStateFactory();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    await action(cancellationToken).ConfigureAwait(continueOnCapturedContext);

                    return;
                }
                catch (Exception ex)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken)
                        {
                            throw;
                        }
                        cancellationToken.ThrowIfCancellationRequested();
                    }

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

                    if (!(await policyState
                          .CanRetryAsync(ex, cancellationToken, continueOnCapturedContext)
                          .ConfigureAwait(continueOnCapturedContext)))
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #8
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;
                    }
                }
            }
        }