Last active
January 14, 2016 21:10
-
-
Save reisenberger/bef379baa0a9d024a8ee to your computer and use it in GitHub Desktop.
Add attempt count to Policy.ExecuteAndCaptureAsync(...), ref Polly #73
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<PolicyResult<TResult>> ExecuteAndCaptureAsync<TResult>(Func<CancellationToken, Task<TResult>> action, CancellationToken cancellationToken, bool continueOnCapturedContext) | |
{ | |
if (_asyncExceptionPolicy == null) throw new InvalidOperationException( | |
"Please use the asynchronous RetryAsync, RetryForeverAsync, WaitAndRetryAsync or CircuitBreakerAsync methods when calling the asynchronous Execute method."); | |
try | |
{ | |
var result = default(TResult); | |
int attempts = 0; // Added | |
await _asyncExceptionPolicy(async ct => | |
{ | |
attempts++; // Added | |
result = await action(ct).ConfigureAwait(continueOnCapturedContext); | |
}, cancellationToken, continueOnCapturedContext) | |
.ConfigureAwait(continueOnCapturedContext); | |
return PolicyResult<TResult>.Successful(result); // Extend to be .Successful(result, attempts) | |
} | |
catch (Exception exception) | |
{ | |
return PolicyResult<TResult>.Failure(exception, GetExceptionType(_exceptionPredicates, exception)); // Extend to add attempts to .Failure(...) signature | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment