Implement Simple Retry pattern in C# – Resiliency

Implement Simple Retry pattern in C

Today in this article we will see an easy-to-use Simple Retry pattern in C# .NET Retry pattern in C# which can be used for executing given business logic/code addressing multiple attempts to retry execution.

If you have a requirement, for adding a resilient logic processing then the code can retry attempting to execute the actions a few more times.

Such retries generally resolve the issues in the case of transient errors or any temporary issues giving the desired result for the request.

Example 1

Here is below a simple example of Retry logic using C# .NET which can be used to execute any of your custom logic bind to an action.

I am using the Console C# application below where I have static methods defined.

Implement Simple Retry pattern in C#, polly retry example c#, retry pattern in c#, retry in catch block c#, c# retry with timeout, c# retry task with delay, c# retry async task, how to retry api call in c#, c waitandretryasync,

Below is how we can use the above Retry method in C# Calle side code

Retry(() =>PerformFileProcessing(filePath), 3);

  • Here in the above example, action is mapped to the method -> PerformFileProcessing is a custom method/action that has some core logic. For example- it performs file operations like read or write.
  • No of retry – For any exceptions method retries execution 3 times

private static void PerformFileProcessing(string filePath)
        {
            using (FileStream fsStrean = File.Create(filePath))
            {
                byte[] info = new UTF8Encoding(true).GetBytes("This is test file.");
                // Add details to the file.
                fsStrean.Write(info1, 0, info.Length);
            }
        }

Example 2


Alternatively, the below logic can be used for performing the retry.

private const int NumberOfRetries = 3;
  for (int i=1; i <= NumberOfRetries; ++i) {
    try {
        // Perfrom your business logic here
        break; // When done, break the  loop
        }
    catch (IOException e) when (i <= NumberOfRetries) {
    }
}

In the catch block, if needed, you can add a time delay to retry with a timeout so that each retry happens with some delay.

Retry logic comes in handy on many occasions especially if you are dealing with shared resources. Most transient errors are common examples of issues related to shared resources.

Such transient errors get auto corrected and hence retrying the logic sometimes resolves the issues.

Before retrying the logic, make sure the object is not in a faulted state and you use the clean object in the logic.

You can use the Polly library to implement the HTTP request retry mechanism easily.

Please visit this article for more details,

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *