■ 재시도 로직을 사용하는 방법을 보여준다.
▶ RetryHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
namespace TestProject; /// <summary> /// 재시도 헬퍼 /// </summary> public static class RetryHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 실행하기 - Execute<TReturn>(action, retryInterval, maximumRetryCount) /// <summary> /// 실행하기 /// </summary> /// <typeparam name="TReturn">리턴 타입</typeparam> /// <param name="action">액션</param> /// <param name="retryInterval">재시도 간격 (단위 : 밀리초)</param> /// <param name="maximumRetryCount">최대 재시도 카운트</param> /// <returns>리턴 값</returns> public static TReturn Execute<TReturn>(Func<TReturn> action, TimeSpan retryInterval, int maximumRetryCount = 3) { List<Exception> exceptionList = new List<Exception>(); for(int i = 0; i < maximumRetryCount; i++) { try { if(i > 0) { Thread.Sleep(retryInterval); } return action(); } catch(Exception exception) { exceptionList.Add(exception); } } throw new AggregateException(exceptionList); } #endregion #region 실행하기 - Execute(action, retryInterval, maximumRetryCount) /// <summary> /// 실행하기 /// </summary> /// <param name="action">액션</param> /// <param name="retryInterval">재시도 간격 (단위 : 밀리초)</param> /// <param name="maximumRetryCount">최대 재시도 카운트</param> public static void Execute(Action action, TimeSpan retryInterval, int maximumRetryCount = 3) { Execute<object> ( () => { action(); return null; }, retryInterval, maximumRetryCount ); } #endregion } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 정상 URL /// </summary> private static readonly string _normalURL = "https://icodebroker.tistory.com"; /// <summary> /// 비정상 URL /// </summary> private static readonly string _abnormalURL = "https://icodebroker1.tistory.com"; /// <summary> /// 요청 URL /// </summary> private static string _requestURL = _abnormalURL; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { HttpClient client = new HttpClient(); int i = 0; RetryHelper.Execute ( () => { Console.WriteLine($"실행 카운트 : {++i}"); try { string content = client.GetStringAsync(_requestURL).GetAwaiter().GetResult(); Console.WriteLine(); Console.WriteLine("실행 결과"); Console.WriteLine("--------------------------------------------------"); Console.WriteLine(content.Substring(0, 100)); Console.WriteLine("--------------------------------------------------"); } catch(Exception) { if(i == 2) // 테스트를 위해 2번째 에러시 요청 URL을 정상 URL로 설정한다. { _requestURL = _normalURL; } throw; } }, TimeSpan.FromSeconds(1), 3 ); } #endregion } |