Skip to content

Instantly share code, notes, and snippets.

@reisenberger
Last active January 14, 2016 21:10
Show Gist options
  • Save reisenberger/bef379baa0a9d024a8ee to your computer and use it in GitHub Desktop.
Save reisenberger/bef379baa0a9d024a8ee to your computer and use it in GitHub Desktop.
Add attempt count to Policy.ExecuteAndCaptureAsync(...), ref Polly #73
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