Polly RetryForever isn't retrying

I’m, trying to check a simple RetryForever of Polly

 class Program
    {
        public static void Main()
        {
               
            int i = 0;
            var _retryPolicy = Policy
           .Handle<Exception>()
           .RetryForever();

            _retryPolicy.Execute(async () =>
            {
                Console.WriteLine(i);
                i++;
                    int.Parse("something");
            });

            Console.ReadLine();
        }
    }

A you can see I’ve created a variable i to track the number of the exestuations

Excepted Result:
print i = 0, 1, 2 etc

Actual Result:
print i = 0

Thank You

>Solution :

I’ve checked your code and you had right, its stops on 0.

But when i removed async keyword (u didnt have any async operation here) the code runs correct.

So, try this:

using Polly;

int i = 0;
var _retryPolicy = Policy
  .Handle<Exception>()
  .RetryForever();

_retryPolicy.Execute(() =>
{
  Console.WriteLine(i);
  i++;
  int.Parse("something");
  return Task.CompletedTask;
});

Leave a Reply