Last active
July 11, 2018 22:42
-
-
Save hnabbasi/bba04322f7f032e0accd47b50587a874 to your computer and use it in GitHub Desktop.
This file contains 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
internal async Task<T> RetryInner<T>(Func<Task<T>> func, | |
int retryCount = 1, | |
Func<Exception, int, Task> onRetry = null) | |
{ | |
var onRetryInner = new Func<Exception, int, Task>((e, i) => | |
{ | |
return Task.Factory.StartNew(() => { | |
#if DEBUG | |
System.Diagnostics.Debug.WriteLine($"Retry #{i} due to exception '{(e.InnerException ?? e).Message}'"); | |
#endif | |
}); | |
}); | |
return await Policy.Handle<Exception>().RetryAsync(retryCount, onRetry ?? onRetryInner).ExecuteAsync<T>(func); | |
} | |
internal async Task<T> WaitAndRetryInner<T>(Func<Task<T>> func, Func<int, TimeSpan> sleepDurationProvider, | |
int retryCount = 1, | |
Func<Exception, TimeSpan, Task> onRetryAsync = null) | |
{ | |
var onRetryInner = new Func<Exception, TimeSpan, Task>((e, t) => | |
{ | |
return Task.Factory.StartNew(() => { | |
#if DEBUG | |
System.Diagnostics.Debug.WriteLine($"Retrying in {t.ToString("g")} due to exception '{(e.InnerException ?? e).Message}'"); | |
#endif | |
}); | |
}); | |
return await Policy.Handle<Exception>().WaitAndRetryAsync(retryCount, sleepDurationProvider, onRetryAsync ?? onRetryInner).ExecuteAsync<T>(func); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment