public TResult Invoke(T arg) { bool tryAgain = true; TResult result = default(TResult); using (var retryAction = new DelayedFunc <T, TResult>(MyFunc)) { while (tryAgain) { Tries++; retryAction.SetParameter(arg); var awaiter = retryAction.RunWithDelayAsync(Strategy.GetNextDelay()) .ConfigureAwait(true) .GetAwaiter(); try { result = awaiter.GetResult(); } catch (Exception e) { Exception = e; } tryAgain = Tries < MaxTries; } retryAction.Cancel(); } return(result); }
private async Task <HttpResponseMessage> RequestWithRetriesAsync(Methods method, string endpoint, string content, CancellationToken cancellationToken = default(CancellationToken)) { var attempts = 0; var response = await RequestAsync(method, endpoint, content, cancellationToken).ConfigureAwait(false); attempts++; while (_retryStrategy.ShouldRetry(attempts, response)) { var timespan = _retryStrategy.GetNextDelay(attempts, response); if (timespan > TimeSpan.Zero) { await Task.Delay(timespan).ConfigureAwait(false); } response = await RequestAsync(method, endpoint, content, cancellationToken).ConfigureAwait(false); attempts++; } return(response); }