রিট্রাই লজিক ইমপ্লিমেন্ট করা হয় যখন কোনো অপারেশন ফেল করে। রিট্রাই লজিক শুধু সেখানে ইমপ্লিমেন্ট করুন যেখানে ফেল হওয়া অপারেশনের পূর্ণ কন্টেক্সট থাকে।
সব কানেক্টিভিটি ফেইলিউর যা রিট্রাই করাকে বাধ্য করে সেগুলো লগ করা গুরুত্বপূর্ণ যাতে অ্যাপ্লিকেশন, সার্ভিস, বা রিসোর্সের অন্তর্নিহিত সমস্যাগুলো চিহ্নিত করা যায়।
উদাহরণ
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);
}
}