2

I am trying to implement within a constructor the Polly for handling HTTP error statuses, but I get an error saying:

The non-generic method cannot be used with type arguments.

I used the code from their website though. Here it is:

 public BaseApiManager()
        {
           Retry = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
                        .OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.BadGateway)
                        .WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(5));
        }

The error is on the second line, at the OrResult. I also tried using it with ApiResponse<T>, which is my generic class, but it does not work as well. What am I doing wrong ?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
MariaAndrews
  • 31
  • 1
  • 2

2 Answers2

1

It should work as follows:

Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
                    .OrResult(r => r.StatusCode == HttpStatusCode.BadGateway)
                    .WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(5));

The .OrResult(...) does not need generic type arguments, because the .Handle<HttpResponseMessage>(...) clause has already bound the policy to handle results of type HttpResponseMessage.

mountain traveller
  • 7,591
  • 33
  • 38
0

Correct your Retry definition specifing type:

private RetryPolicy<HttpResponseMessage> Retry { get; }