Often, we have transient problems in our application, such as a network failure, system rebooting, or anything else that throws an exception. In this article, we are going to learn how to implement retry logic in C# to help us handle these problems.
Let’s start.
Simulate a Failure in Our Application
Before we create the retry logic in C#, we need a method that can create a transient problem. That said, let’s create a very basic logic to simulate the failure:
First, this method asks for a number. After parsing this number into an integer, it compares this number to a forbidden number we previously set.
If the input number is equal to the forbidden number, this method is going to throw an exception with a message, otherwise, it prints the “Not Equal” message in the console.
This exception simulates a transient problem that may occur by a brief network failure or something like this.
Creating the Second Method
Let’s inspect the second method:
Note that this method is very similar to the FirstSimulationMethod, however, in the end, it returns the input value. This method is important to show how to implement retry logic using Action or Func delegates.
Implementing the Retry Logic in C#
Once we have the methods to simulate the transient problems, we can focus on writing the retry logic in C#. Let’s create an Executor static class with an Execute method:
The Execute method is responsible to execute the logic several times if there’s any problem. It receives an Action as a first parameter and the number of times we want to retry (numberOfRetries) as a second parameter.
Then, we need to loop and execute the method until the tries variable value is lower or equal to the numberOfRetries variable value. If the Action executes successfully, the retry logic finishes its execution. However, in case of exception, it increments the tries variable and retries to execute the Action.
When the tries value is greater or equal than the numberOfRetries, it finishes the execution and throws an exception with some message.
Retry Logic in C# With the Func Delegate
Now, let’s create a new overload of the Execute method:
Note that the main difference is that this time this is a generic method with a return type.
Once it has a return type, instead of receiving an Action as a parameter, this method receives a Func, and then, we return the result of this Func.
With both previous methods, we can use this retry logic in C# for both, Action and Func delegates.
Using the Executor Class
Once we have defined the Executor class and its methods, it is time to execute the FirstSimulationMethod and the SecondSimulationMethod methods.
Let’s check it:
We call the Execute method under the Executor class passing the method we want to execute, and the maximum number of retries as parameters.
Now, let’s use the overload method:
This time, we pass a Func as the first parameter. That said, we need to declare a result variable to receive the return from this Func.
It is good to mention that when we send 3 as the number of retries, this method is going to execute 4 times. The first execution and three retries.
We can Implement Retry logic with Polly https://www.pollydocs.org
0 comments:
Post a Comment