I have the following configuration of my HTTP service:
services
.AddHttpClient<ISomeHttpService, SomeHttpService>((Action<HttpClient>) (client =>
{
client.BaseAddress = new Uri(configuration.BaseAddress);
client.Timeout = TimeSpan.FromSeconds(10);
}))
.AddPolicyHandler(GetPollyPolicy())
.AddHttpMessageHandler<DistributedCachedHttpClientHandler>();
// .....
private static IAsyncPolicy<HttpResponseMessage> GetPollyPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrInner<Exception>() // I will probably use TimeoutException, for now I'm checking all possible options
.WaitAndRetryAsync(2, _ => TimeSpan.FromMilliseconds(100),
(exception, timeSpan) =>
{
_ = "breakpoint";
});
}
It works really well, if the request made by SomeHttpService
returns 500. Polly tries to retry 2 more times.
However, if the request takes more time than 10 seconds (which is more than the configured timeout), retries do not occur. I even set a breakpoint within the Polly's action and I see that the breakpoint is hit when the timeout occurs! However, when I continue the program, no retries are executed and my web API fails with 500 directly after.
What's wrong?
I also tried using Or<Exception>()
instead of OrInner<Exception>()
.