I have defined some Typed API Client via Dependency Injection in Start up like below.
public void ConfigureHttpClients(IServiceCollection services)
{
services.AddHttpClient<IMyHttpClient,MyHttpClient>()
.AddPolicyHandler(GetRetryPolicy(Configuration));
}
And Get Retry Policy is defined like this :
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(IConfiguration configuration)
{
int numberOfretries = Convert.ToInt32(configuration["PollyRetries"]); // Value is set to 3
TimeSpan delay = TimeSpan.FromSeconds(Convert.ToInt32(configuration["PollyMaxDelaySeconds"])); // Delay is set to 3
return HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(
numberOfretries,
retryAttempt => delay,
onRetry: (response, span, retryCount, context) =>
{
Log.Information("Attempt # {retryCount}", retryCount);
});
}
Retry Count is set to 3 and Delay is set to 3 seconds.
MyHttpClient is defined like this :
public class MyHttpClient : IMyHttpClient
{
public MyHttpClient (HttpClient httpClient, IMemoryCache cache)
{
_httpClient = httpClient,
_cache = cache
}
}
But from the below image you can see even though the max retry is set to 3 the retry count is getting reset after 3 attempts and continues to retry until timeout
The Packages I am using related to Polly are -
<PackageReference Include="Microsoft.Extensions.Http.Polly" />
<PackageReference Include="Polly.Extensions.Http" />
Why Polly is retrying even though max limit is reached?
What can I do to resolve this ?
I have tried changing the package versions. But it did not help. Also looked at the Microsoft Documentation for Polly Implementation - implement-http-call-retries-exponential-backoff-polly. The implementation is exactly same as I have mentioned before.
If I wrap my client call with policy execute method like this :
_resiliencyPolicy = HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(3));
await _resiliencyPolicy.ExecuteAsync(async () =>
{
await _myHttpClient.PostAsync(payload).ConfigureAwait(false);
}).ConfigureAwait(false);
Then it only retries for 3 times after initial call. But configuring the HttpClient
directly in startup.cs with policy, it continues to retry