Skip to content

Instantly share code, notes, and snippets.

@stephenpatten
Forked from benhysell/retry.cs
Created May 11, 2018 19:27
Show Gist options
  • Save stephenpatten/96f22c3b72015d69eb4cc8281bfc7606 to your computer and use it in GitHub Desktop.
Save stephenpatten/96f22c3b72015d69eb4cc8281bfc7606 to your computer and use it in GitHub Desktop.
Polly - Back Off and Retry on Error http client
//Retry http with a back off on failure
await Policy.Handle<HttpRequestException>().WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.ExecuteAsync(async () =>
{
var result = await httpClient.GetAsync("api/CallToServer/" + varibleToSendToServer, cancellationToken);
if (result.IsSuccessStatusCode)
{
returnValue = await result.Content.ReadAsAsync<List<Results>>(token);
}
else if (result.StatusCode == HttpStatusCode.Unauthorized)
{
throw new UnauthorizedAccessException("Your Session has expired.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment