using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
public static async Task Main()
private static ValueTask<bool> HandleTransientHttpError(Outcome<HttpResponseMessage> outcome)
{ Exception: HttpRequestException } => PredicateResult.True(),
{ Result.StatusCode: HttpStatusCode.RequestTimeout } => PredicateResult.True(),
{ Result.StatusCode: >= HttpStatusCode.InternalServerError } => PredicateResult.True(),
_ => PredicateResult.False()
private static RetryStrategyOptions<HttpResponseMessage> GetRetryOptions()
ShouldHandle = args => HandleTransientHttpError(args.Outcome),
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromSeconds(2)
public static async Task RefitExample()
#region http-client-integrations-refit
ServiceCollection services = new();
services.AddRefitClient<IHttpStatusApi>()
.ConfigureHttpClient(client => client.BaseAddress = new Uri("https://httpstat.us/"))
.AddResilienceHandler("refit_based_pipeline",
builder => builder.AddRetry(GetRetryOptions()));
var provider = services.BuildServiceProvider();
var apiClient = provider.GetRequiredService<IHttpStatusApi>();
var response = await apiClient.GetRequestTimeoutEndpoinAsync();
public interface IHttpStatusApi
Task<HttpResponseMessage> GetRequestTimeoutEndpoinAsync();