0
A Fallback strategy allows your system to handle errors gracefully by providing default data, cached content, or alternative behavior when an operation fails.
Configuring Fallback in Polly v8
Fallback policies capture exceptions or specific result conditions and return a substitute response.
Code Example
var pipeline = new ResiliencePipelineBuilder<string>()
.AddFallback(new FallbackStrategyOptions<string>
{
ShouldHandle = new PredicateBuilder<string>().Handle<HttpRequestException>(),
FallbackAction = args =>
{
// Return default fallback value
return Outcome.FromResultAsValueTask("Default fallback content");
}
})
.Build();
string content = await pipeline.ExecuteAsync(async ct =>
{
return await FetchOnlineContentAsync(ct);
});