Search code examples
c#.netdotnet-httpclientpollyretry-logic

After a 500 response no requests are sent again until process is restarted


I am using Polly to implement request retries. However, after a 500 response is returned for a request, not only does Polly not send a request when it retries but no requests are sent at all until the process is restarted. Instead Polly just waits until there is a timeout. The following is the code:

var timeoutPolicy = TimeoutPolicy
    .TimeoutAsync<HttpResponseMessage>(c => c.GetTimeout() ?? TimeSpan.FromSeconds(30)); //timeout para cada intento

var retryPolicy = HttpPolicyExtensions
    .HandleTransientHttpError()
    .Or<TimeoutRejectedException>() //tirado por la TimeoutPolicy
    .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) });

var policyHandler = new PolicyHandler(Policy.WrapAsync(retryPolicy, timeoutPolicy));

How can I fix the code such that Polly actually sends the request when it retries after a 500 and also for following requests?


Solution

  • The error was caused by the following issue

    This is how I fixed it:

    var retryPolicy = HttpPolicyExtensions
        .HandleTransientHttpError()
        .Or<TimeoutRejectedException>() //tirado por la TimeoutPolicy
        .WaitAndRetryAsync(new[] 
          { 
            TimeSpan.FromSeconds(1), 
            TimeSpan.FromSeconds(5), 
            TimeSpan.FromSeconds(10) 
          }, 
          onRetry: (x, _) =>
          {
              //dispose response when retrying
              x.Result?.Dispose();
          });