MariaAndrews
MariaAndrews

Reputation: 31

C# Polly for handling error http responses

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 ?

Upvotes: 2

Views: 4226

Answers (2)

mountain traveller
mountain traveller

Reputation: 8156

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.

Upvotes: 1

Grzesiek Danowski
Grzesiek Danowski

Reputation: 467

Correct your Retry definition specifing type:

private RetryPolicy<HttpResponseMessage> Retry { get; }

Upvotes: 0

Related Questions