May 19 2025
dotnet add package Microsoft.Extensions.Http.Resilience
dotnet add package Microsoft.Extensions.Resilience
ResiliencePipeline pipeline = new()
.AddRetry(new RetryStrategyOptions
{
ShouldHandle = new PredicateBuilder().Handle<ConflictException>(),
Delay = TimeSpan.FromSeconds(2),
MaxRetryAttempts = 3,
BackoffType = DelayBackoffType.Exponential,
UseJitter = true
})
.AddTimeout(new TimeoutStrategyOptions
{
Timeout = TimeSpan.FromSeconds(5)
})
.Build();
await pipeline.ExecuteAsync(
async ct => await httpClient.GetAsync("/api/weather", ct),
cancellationToken);
builder.Services.AddResiliencePipeline("retry-pipeline", builder =>
{
builder.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 3,
Delay = TimeSpan.FromMilliseconds(300),
BackoffType = DelayBackoffType.Exponential,
ShouldHandle = new PredicateBuilder().Handle<HttpRequestException>()
});
});
builder.Services.AddResiliencePipeline("timeout-pipeline", builder =>
{
builder.AddTimeout(TimeSpan.FromSeconds(2));
});
builder.Services.AddResiliencePipeline("cb-pipeline", builder =>
{
builder.AddCircuitBreaker(new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
MinimumThroughput = 10,
SamplingDuration = TimeSpan.FromSeconds(30),
BreakDuration = TimeSpan.FromSeconds(15)
});
});
builder.Services.AddResiliencePipeline<string, string>("gh-hedging", builder =>
{
builder.AddHedging(new HedgingStrategyOptions<string>
{
MaxHedgedAttempts = 3,
DelayGenerator = args =>
{
var delay = args.AttemptNumber switch
{
0 or 1 => TimeSpan.Zero, // Parallel mode
_ => TimeSpan.FromSeconds(-1) // Fallback mode
};
return new ValueTask<TimeSpan>(delay);
}
});
});
builder.Services.AddResiliencePipeline<string, string?>("gh-fallback", builder =>
{
builder.AddFallback(new FallbackStrategyOptions<string?>
{
FallbackAction = _ =>
Outcome.FromResultAsValueTask<string?>(string.Empty)
});
});
builder.Services.AddResiliencePipeline("ratelimiter-pipeline", builder =>
{
builder.AddRateLimiter(new SlidingWindowRateLimiter(
new SlidingWindowRateLimiterOptions
{
PermitLimit = 100,
SegmentsPerWindow = 4,
Window = TimeSpan.FromMinutes(1)
}
));
});
app.MapGet("/subscribers", async (
HttpClient httpClient,
ResiliencePipelineProvider<string> pipelineProvider,
CancellationToken cancellationToken) =>
{
var pipeline = pipelineProvider.GetPipeline<Subscriber?>("gh-fallback");
return await pipeline.ExecuteAsync(
async token =>
await httpClient.GetFromJsonAsync<Subscriber>("api/subscribers", token),
cancellationToken);
});
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 15,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.