Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic method to execute any method with 1 time retry using Delegates

Tags:

c#

.net

delegates

I am trying to develop a mechanism using which I can execute any method with 1 retry attempt.

The retry will be triggered if an exception is encountered in the 1st run.

The basic Idea is, I will have a generic class for the retry logic, and I want to pass any method in it via delegates. And that method will be executed with 1 retry.

So far I have developed this.

public class RetryHandler
{
    Delegate _methodToRetry;

    // default constructor with delegate to function name.
    public RetryHandler(Delegate MethodToExecuteWithRetry)
    {
        _methodToRetry = MethodToExecuteWithRetry;
    }

    public void ExecuteWithRetry(bool IsRetryEnabled)
    {
        try
        {
            _methodToRetry.DynamicInvoke();
        }
        catch (Exception ex)
        {
            if (IsRetryEnabled)
            {
                // re execute method.
                ExecuteWithRetry(false);
            }
            else
            {
                // throw exception
                throw;
            }
        }

    }


}

Now I have a problem:

The methods which I want to pass have different Input parameters (both by number of parameters and object types) and different output parameters.

Is there any way by which I can achieve this? Basically I want to call the methods like this:

RetryHandler rh = new RetryHandler(MyMethod1(int a, int b));
int output = (int) rh.ExecuteWithRetry(true);

RetryHandler rh2 = new RetryHandler(MyMethod2(string a));
string output2 = (string) rh2.ExecuteWithRetry(true);

Any help would be much appreciated. Thank you.

like image 954
Bluemarble Avatar asked Feb 08 '18 12:02

Bluemarble


People also ask

What are the three types of generic delegates in C#?

Func, Action and Predicate are generic inbuilt delegates present in System namespace. All three can be used with method, anonymous method and lambda expression.

What are the generic delegates you have used?

Func, Action, and Predicate are Generic Inbuilt delegates that are present in the System namespace which is introduced in C# 3. All these three delegates can be used with the method, Anonymous Method, and Lambda Expressions in C#.

What is generic delegates?

A delegate can define its own type parameters. Code that references the generic delegate can specify the type argument to create a closed constructed type, just like when instantiating a generic class or calling a generic method, as shown in the following example: C# Copy.


1 Answers

You an wrap the call:

public T Retry<T>(Func<T> func)
{
    try { return func(); }
    catch { return func(); }
}

This will let you to call anything returning a value:

public int Test(int a, int b) => a + b;
public string Test(string a) => a + a;

void Example()
{
    Console.WriteLine(Retry(() => Test(1, 2)));
    Console.WriteLine(Retry(() => Test("a")));
}
like image 67
Sinatr Avatar answered Sep 20 '22 05:09

Sinatr