1

I'm very new to C# coding and I just want to know how to setup polly WaitAndRetry for my function if it failed. Following is my steps

  1. I installed package Install-Package Polly, using NuGet package
  2. added using polly in my code.
  3. Below is my code
public async Task<string> ConfigInsert(config model)
{
    try
    {
        SendToDatabase(model);

        await Policy.Handle<Exception>()
            .RetryAsync(NUMBER_OF_RETRIES)
            .ExecuteAsync(async () =>
                await SendToDatabase(model))
            .ConfigureAwait(false);
    } 
    Catch(Exception e)
    {
        _log.write("error occurred");
    }
        
    public async Task<string> SendToDataBase(config model)
    {
        var ss = DataBase.PostCallAsync(model)
            .GetAwaiter()
            .GetResult();
        return ss;
    }
}

But this call is continuously calling without any delay. I tried to use WaitAndRetryAsync in catch call but it's not working. WaitAndRetryAsync accepts only HTTP repose message. I want to implement ait and retry option in try-catch

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
june alex
  • 244
  • 4
  • 17
  • I'm not hugely familiar with Polly but isn't the point of it, that you can not bother about the whole try catch like you have here you just confiugre polly (maybe like you have maybe not) and then call it, Polly handles failures and reties and if it works the first time then great it will return the result – Dave Aug 27 '20 at 12:09
  • ok thanks .i will remove try catch – june alex Aug 27 '20 at 12:16
  • 1
    @junealex Please try to avoid to use `GetAwaiter().GetResult()`. Please prefer `await` instead. – Peter Csala Aug 27 '20 at 12:27

1 Answers1

5

You say you want WaitAndRetry but you don't use that function... And it doesn't only work with HttpResponse. Please read the documentation.

The code below should give you a head start:

class Program
{
    static async Task Main(string[] args)
    {
        // define the policy using WaitAndRetry (try 3 times, waiting an increasing numer of seconds if exception is thrown)
        var policy = Policy
          .Handle<Exception>()
          .WaitAndRetryAsync(new[]
          {
            TimeSpan.FromSeconds(1),
            TimeSpan.FromSeconds(2),
            TimeSpan.FromSeconds(3)
          });

        // execute the policy
        await policy.ExecuteAsync(async () => await SendToDatabase());

    }

    static async Task SendToDatabase()
    {
        Console.WriteLine("trying to send to database");
        await Task.Delay(100);
        throw new Exception("it failed!");
    }
}
jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • no try catch required right ?.Also how to store the return value in ExecuteAsync? var info = await policy.ExecuteAsync(async () => await SendToDatabase()); this way right ? – june alex Aug 27 '20 at 12:51
  • 1
    The policy only handles exceptions thrown by the execute delegate while it is retrying. If the final retry attempt fails then an exception will be thrown, so you may still want the try catch around `ExecuteAsync` to handle this scenario. See [the docs](https://github.com/App-vNext/Polly/wiki/Retry#how-polly-retry-works) for an explanation. – rob.earwaker Aug 31 '20 at 05:30