2

I am new to Polly library and trying to use this library to retry connectivity to service bus on failures.

  • I am using WaitAndRetryAsync to attempt retry for a specific times within intervals.
  • There are 2 producer (mediatr events) who connect to service bus. I am throwing an exception in the connect method and this works as expected for the producer 1. The WaitAndRetryAsync is triggered as expected.
  • After this the producer 2 calls the same connect method and the same exception occurs. This time the breakpoint goes into the Handle<Exception> but the WaitAndRetryAsync is never called.

Kindly guide me what am I doing wrong here.

This is my connection retry policy

private readonly AsyncRetryPolicy<IConnection> _connectionRetryPolicy;

In constructor I set the policy

_connectionRetryPolicy = CreateConnectionRetryPolicy(recoveryPolicy);
private AsyncRetryPolicy<IConnection> CreateConnectionRetryPolicy(IRecoveryPolicy recoveryPolicy)
{
    return Policy<IConnection>
        .Handle<Exception>(exceptionPredicate: exception =>
        {
            Console.WriteLine(exception);
            return true;
        })
        .WaitAndRetryAsync(recoveryPolicy.RetryCount, (retryAttempt, context) =>
        {
            context.SetRetryCount(retryAttempt);
            return recoveryPolicy.GetDelay(retryAttempt);
        }, (result, _, context) =>
        {
            var retryCount = context.GetRetryCount();
            var endpoint = context.GetEndpoint();
        });
}

This is my execute logic

private Task<IConnection> CreateConnection(CancellationToken cancellationToken = default)
{
    var ctx = new Context();
    ctx.SetRetryCount(0);
    return _connectionRetryPolicy.ExecuteAsync(async (context, ct) =>
    {
        var connection = await CreateConnectionAsync(endpoint, ct);
        return connection;
    }, ctx, cancellationToken);
}  
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
V R
  • 33
  • 3

1 Answers1

2

You should recreate the policy whenever you call the CreateConnection and not reuse it.

var retryPolicy = CreateConnectionRetryPolicy(recoveryPolicy)
return await retryPolicy.ExecuteAsync(async () => { ... });

The Handle is called because it is part of the evaluation process. But most probably you have already exceeded the max retry threshold and that's why you are not firing the retry.

Please also bear in mind that onRetry/onRetryAsync delegates inside the WaitAndRetryAsync should be used for logging not the Handle.

  • Handle is called (part of the evaluation) every time even is the policy will not trigger
  • onRetry/onRetryAsync is called only when the policy should trigger but before the sleep duration and the next attempt
Peter Csala
  • 17,736
  • 16
  • 35
  • 75