- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write retry logic in C#?
Retry logic is implemented whenever there is a failing operation. Implement retry logic only where the full context of a failing operation.
It's important to log all connectivity failures that cause a retry so that underlying problems with the application, services, or resources can be identified.
Example
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); } }
Advertisements
To Continue Learning Please Login
Login with Google