>  기사  >  백엔드 개발  >  C#에서 재시도 논리를 작성하는 방법은 무엇입니까?

C#에서 재시도 논리를 작성하는 방법은 무엇입니까?

王林
王林앞으로
2023-09-24 12:45:031311검색

C#에서 재시도 논리를 작성하는 방법은 무엇입니까?

작업이 실패하는 한 재시도 로직이 구현됩니다. 재시도 구현 실패한 작업의 전체 컨텍스트 내에서만 논리를 문서화하세요.

재시도를 초래하는 모든 연결 실패를 기록하는 것이 중요합니다. 애플리케이션, 서비스 또는 리소스의 문제를 식별할 수 있습니다.

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);
   }
}

위 내용은 C#에서 재시도 논리를 작성하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제