Skip to content

Instantly share code, notes, and snippets.

@Edkorenkov
Last active July 22, 2022 17:40
Show Gist options
  • Save Edkorenkov/a53a292f4ce64a7a55eace9b96286d4b to your computer and use it in GitHub Desktop.
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
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