<PackageReference Include="Polly" Version="3.0.0" />

RetryPolicyStateWithCount

using System; using System.Threading; using System.Threading.Tasks; namespace Polly.Retry { internal class RetryPolicyStateWithCount : IRetryPolicyState { private int _errorCount; private readonly int _retryCount; private readonly Action<Exception, int, Context> _onRetry; private readonly Context _context; public Task<bool> CanRetryAsync(Exception ex, CancellationToken ct, bool continueOnCapturedContext) { return Task.FromResult(CanRetry(ex)); } public RetryPolicyStateWithCount(int retryCount, Action<Exception, int, Context> onRetry, Context context) { _retryCount = retryCount; _onRetry = onRetry; _context = context; } public RetryPolicyStateWithCount(int retryCount, Action<Exception, int> onRetry) : this(retryCount, delegate(Exception exception, int i, Context context) { onRetry(exception, i); }, null) { } public bool CanRetry(Exception ex) { _errorCount++; bool num = _errorCount <= _retryCount; if (num) _onRetry(ex, _errorCount, _context); return num; } } }