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 theWaitAndRetryAsync
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);
}