재시도 논리는 실패한 작업이 있을 때마다 구현됩니다. 실패한 작업의 전체 컨텍스트에서만 재시도 논리를 구현합니다.
애플리케이션, 서비스 또는 리소스의 근본적인 문제를 식별할 수 있도록 재시도를 유발하는 모든 연결 실패를 기록하는 것이 중요합니다.
예시
class Program{
public static void Main(){
HttpClient client = new HttpClient();
dynamic res = null;
var retryAttempts = 3;
var delay = TimeSpan.FromSeconds(2);
RetryHelper.Retry(retryAttempts, delay, () =>{
res = client.GetAsync("https://example22.com/api/cycles/1");
});
Console.ReadLine();
}
}
public static class RetryHelper{
public static void Retry(int times, TimeSpan delay, Action operation){
var attempts = 0;
do{
try{
attempts++;
System.Console.WriteLine(attempts);
operation();
break;
}
catch (Exception ex){
if (attempts == times)
throw;
Task.Delay(delay).Wait();
}
} while (true);
}
}