public static async Task Retry(this IRetryPolicy retryPolicy, Func <Task> retryMethod, CancellationToken cancellationToken = default)
        {
            var inlinePipeContext = new InlinePipeContext(cancellationToken);

            using (RetryPolicyContext <InlinePipeContext> policyContext = retryPolicy.CreatePolicyContext(inlinePipeContext))
            {
                try
                {
                    await retryMethod().ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    if (!policyContext.CanRetry(exception, out RetryContext <InlinePipeContext> retryContext))
                    {
                        throw;
                    }

                    try
                    {
                        await Attempt(inlinePipeContext, retryContext, retryMethod).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException ex) when(ex.CancellationToken == cancellationToken)
                    {
                    }

                    throw;
                }
            }
        }
Esempio n. 2
0
        public static async Task <T> Retry <T>(this IRetryPolicy retryPolicy, Func <Task <T> > retryMethod,
                                               CancellationToken cancellationToken = default(CancellationToken))
        {
            var inlinePipeContext = new InlinePipeContext(cancellationToken);

            using (RetryPolicyContext <InlinePipeContext> policyContext = retryPolicy.CreatePolicyContext(inlinePipeContext))
            {
                try
                {
                    return(await retryMethod().ConfigureAwait(false));
                }
                catch (Exception exception)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        if (exception is OperationCanceledException canceledException &&
                            canceledException.CancellationToken == cancellationToken)
                        {
                            throw;
                        }

                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    if (!policyContext.CanRetry(exception, out RetryContext <InlinePipeContext> retryContext))
                    {
                        throw;
                    }

                    return(await Attempt(inlinePipeContext, retryContext, retryMethod).ConfigureAwait(false));
                }
            }
        }
        public static async Task Retry(this IRetryPolicy retryPolicy, Func <Task> retryMethod, CancellationToken cancellationToken = default(CancellationToken))
        {
            var inlinePipeContext = new InlinePipeContext(cancellationToken);
            RetryPolicyContext <InlinePipeContext> policyContext = retryPolicy.CreatePolicyContext(inlinePipeContext);

            try
            {
                await retryMethod().ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    var canceledException = exception as OperationCanceledException;
                    if (canceledException != null && canceledException.CancellationToken == cancellationToken)
                    {
                        throw;
                    }

                    cancellationToken.ThrowIfCancellationRequested();
                }

                RetryContext <InlinePipeContext> retryContext;
                if (!policyContext.CanRetry(exception, out retryContext))
                {
                    throw;
                }

                await Attempt(inlinePipeContext, retryContext, retryMethod).ConfigureAwait(false);
            }
        }