Currently I have this request:
await url
.SetQueryParams(queryString)
.SetClaimsToken()
.GetJsonAsync<T>()
I'd like to start using Polly (https://github.com/App-vNext/Polly) now to handle retries and provide a better user experience. For instance, not "hanging up" on the user on the first attempt due to bad network connection. This is the example I was trying to use:
int[] httpStatusCodesWorthRetrying = { 408, 500, 502, 503, 504 };
Policy
.Handle<HttpException>()
.OrResult<HttpResponse>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
.WaitAndRetryAsync(new[] {
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
})
.ExecuteAsync( await url... )
But it requires HttpResponse
to be the return type. As you can see from my Flurl example, it's returning T
, even though it is an HttpResponse
. The T
is just the type used to deserialize the StringContent
.
This first example is not working at all since I'm using it inside a PCL and I can't get a reference to System.Web
there. So I tried this:
Policy
.HandleResult(HttpStatusCode.InternalServerError)
.OrResult(HttpStatusCode.BadGateway)
.OrResult(HttpStatusCode.BadRequest)
.WaitAndRetryAsync(new[] {
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
})
.ExecuteAsync(async () =>
{
await url...
});
But this one also doesn't work because Polly expects HttpStatusCode
as return type. So my question is: How can I tell polly to handle those HttpStatusCode
s and still allow my return of type T
?