Last active
July 22, 2022 17:40
-
-
Save Edkorenkov/a53a292f4ce64a7a55eace9b96286d4b to your computer and use it in GitHub Desktop.
Example of how to use .NET Refit Http Client with Polly Retry Policy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var retryPolicy = Policy | |
.Handle<ApiException>() | |
.WaitAndRetryAsync(3, x => TimeSpan.FromSeconds(Math.Pow(2, x))) | |
.AsAsyncPolicy<HttpResponseMessage>(); | |
services | |
.AddRefitClient<IHttpClient>() | |
.ConfigureHttpClient(x => x.BaseAddress = new Uri("https://localhost:8080")) | |
.AddPolicyHandler(retryPolicy); | |
services | |
.AddRefitClient<IHttpClient>() | |
.ConfigureHttpClient(x => x.BaseAddress = new Uri("https://localhost:8080")) | |
.AddTransientHttpErrorPolicy(builder => builder | |
.WaitAndRetryAsync(new[] | |
{ | |
TimeSpan.FromSeconds(1), | |
TimeSpan.FromSeconds(2), | |
TimeSpan.FromSeconds(4) | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment