Скопировано дословно со страницы сети разработчиков Microsoft на то, что они называют Шаблон повторов :
private int retryCount = 3;
...
public async Task OperationWithBasicRetryAsync()
{
int currentRetry = 0;
for (; ;)
{
try
{
// Calling external service.
await TransientOperationAsync();
// Return or break.
break;
}
catch (Exception ex)
{
Trace.TraceError("Operation Exception");
currentRetry++;
// Check if the exception thrown was a transient exception
// based on the logic in the error detection strategy.
// Determine whether to retry the operation, as well as how
// long to wait, based on the retry strategy.
if (currentRetry > this.retryCount || !IsTransient(ex))
{
// If this is not a transient error
// or we should not retry re-throw the exception.
throw;
}
}
// Wait to retry the operation.
// Consider calculating an exponential delay here and
// using a strategy best suited for the operation and fault.
Await.Task.Delay();
}
}
// Async method that wraps a call to a remote service (details not shown).
private async Task TransientOperationAsync()
{
...
}
Они более подробно объясняют, как правильно использовать, а как неправильно.этот шаблон.Например, если вы ожидаете, что ошибки, с которыми вы сталкиваетесь, являются временными, и повторная попытка через мгновение, скорее всего, будет успешной, это может быть для вас.Если это поможет вам справиться с некоторыми проблемами масштабирования, это не для вас.
Возможно, вас также заинтересует их Схема автоматического выключателя , которую они описывают как способную "Обрабатыватьошибки, которые могут занимать различное время для устранения при подключении к удаленному сервису или ресурсу. "