Call a Function With Retry Using Generics


What if you like to call a web service and retry the call again until response? let me show you a nice way to call a function with retry until you retrieve what ever is needed from the function. This could be achieved by using generics especially by extending the Func<T> delegate; check the code below;

   1: public static class MyExtensions
   2:     {
   3:         public static T WithRetry<T>(this Func<T> action)
   4:         {
   5:             var result = default(T);
   6:             int retryCount = 0;
   7:             var succesful = false;
   8:             do
   9:             {
  10:                 try
  11:                 {
  12:                     result = action();
  13:                     succesful = true;
  14:                 }
  15:                 catch (Exception ex)
  16:                 {
  17:                     retryCount++;
  18:                 }
  19:             } while (retryCount < 3 && !succesful);
  20:             return result;
  21:         }
  22:     }

In the code you can see that we extended the Func that will return an object with type T. This function will keep trying for 3 times but you might change it to keep it trying until it achieve the goal. and here is how to use it


   1: MyService ser = new MyService();
   2: Func<MyService.contentSetList> fCon = () => ser.get(testlist.ToArray<string>());
   3: var con = fCon.WithRetry();
   4: return con;


hope this helps.

References: the original code belongs to Scott Allen at his course C# Fundamentals - Part 2 @ http://www.pluralsight-training.net. thanks Scott.

4 Comments

  • Or simply using Reactive Extensions

    Observable.Start(service.GetClients).Retry().Subscribe(_ =&gt; /* value */, err =&gt; /* exception */, () =&gt; /* completed */);

  • Benjamin; thank you for your contribution. but the Reactive Extension is for asynchronous more then retrying to recall the function. but the code in this post is to call any function until the function objectives achieved.

  • You're swallowing the exception, which will make it hard to diagnose any problems. Try something like this instead:

    public static T WithRetry&lt;T&gt;(this Func&lt;T&gt; action)

    {

    &nbsp; &nbsp;byte retryCount = 0;

    &nbsp; &nbsp;while (true)

    &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp;try

    &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return action();

    &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp;catch (Exception)

    &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (3 == retryCount++)

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp;}

    }

  • RichardD; a nice approach... here in the post i did not do every thing i just want to show you the simplest way to do that ! and what ever belongs to you. you can edit the code as you like....

Comments have been disabled for this content.