示例#1
0
        public static T Run <T>(Func <T> func, IRetryStrategy strategy)
        {
            var failCount = 0;

            while (true)
            {
                try
                {
                    return(func.Invoke());
                }
                catch (Exception ex)
                {
                    failCount++;
                    if (!strategy.ShouldRetry(ex is AggregateException ? ex.InnerException : ex, failCount))
                    {
                        throw;
                    }
                }
                Task.Delay(strategy.NextWait(failCount));
            }
        }
示例#2
0
        public static async Task <T> RunAsync <T>(Func <Task <T> > func, IRetryStrategy strategy)
        {
            var failCount = 0;

            while (true)
            {
                try
                {
                    return(await func.Invoke().ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    failCount++;
                    if (!strategy.ShouldRetry(ex is AggregateException ? ex.InnerException : ex, failCount))
                    {
                        throw;
                    }
                }
                await Task.Delay(strategy.NextWait(failCount)).ConfigureAwait(false);
            }
        }