The GetStringAsync returns a Task<string> so you can't examine the reponse's StatusCode.
So, you need to use GetAsync which returns a Task<HttpResponseMessage>.
Because the request uri is the only thing which is changing between the calls that's why you need to receive that as a parameter:
private static HttpClient client = new HttpClient(); //or use IHttpClientFactory
static async Task<HttpResponseMessage> PerformRequest(string uri)
{
Console.WriteLine(uri);
return await client.GetAsync(uri);
}
In order to have a parameter-less action which can be performed by the retry policy we need an address iterator and a wrapper around the PerformRequest:
static IEnumerable<string> GetAddresses()
{
yield return "https://test.com";
yield return "https://test.com?key=XXX";
...
}
private static readonly IEnumerator<string> UrlIterator = GetAddresses().GetEnumerator();
static async Task<HttpResponseMessage> GetNewAddressAndPerformRequest()
{
if (UrlIterator.MoveNext())
return await PerformRequest(UrlIterator.Current);
return null;
}
Each time when you call the GetNewAddressAndPerformRequest it retrieves the next fallback url and then executes the request against that.
What's left is the retry policy itself:
var retryPolicyForNotSuccessAnd4xx = Policy
.HandleResult<HttpResponseMessage>(response => response != null && !response.IsSuccessStatusCode)
.OrResult(response => response != null && (int)response.StatusCode > 400 && (int)response.StatusCode < 500)
.WaitAndRetryForeverAsync(_ => TimeSpan.FromSeconds(1));
- If the
GetNewAddressAndPerformRequest returns null because we have run out of fallback urls then we exit from the retry
- If the statusCode is between 200 and 299 then we exit from the retry
- If the statusCode is between 300 and 400 or greater than 500 then we exit from the retry
- In every other case we perform a retry
The usage could look like this:
var response = await retryPolicyForNotSuccessAnd4xx.ExecuteAsync(async () => await GetNewAddressAndPerformRequest());
if (response == null)
{
Console.WriteLine("All requests failed");
Environment.Exit(1);
}
Console.WriteLine(await response.Content.ReadAsStringAsync());
For the sake of completeness here is the full source code:
class Program
{
private static HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
var retryPolicyForNotSuccessAnd4xx = Policy
.HandleResult<HttpResponseMessage>(response => response != null && !response.IsSuccessStatusCode)
.OrResult(response => response != null && (int)response.StatusCode > 400 && (int)response.StatusCode < 500)
.WaitAndRetryForeverAsync(_ => TimeSpan.FromSeconds(1));
var response = await retryPolicyForNotSuccessAnd4xx.ExecuteAsync(async () => await GetNewAddressAndPerformRequest());
if (response == null)
{
Console.WriteLine("All requests failed");
Environment.Exit(1);
}
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
static IEnumerable<string> GetAddresses()
{
yield return "https://test.com";
yield return "https://test.com?key=XXX";
}
private static readonly IEnumerator<string> UrlIterator = GetAddresses().GetEnumerator();
static async Task<HttpResponseMessage> GetNewAddressAndPerformRequest()
=> UrlIterator.MoveNext() ? await PerformRequest(UrlIterator.Current) : null;
static async Task<HttpResponseMessage> PerformRequest(string uri)
{
Console.WriteLine(uri);
return await client.GetAsync(uri);
}
}