Microsoft.Extensions.Http.Polly 10.0.0
About
Microsoft.Extensions.Http.Polly integrates IHttpClientFactory with the Polly library to provide comprehensive resilience and transient fault-handling. It allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
Note
This package is deprecated. Please use either Microsoft.Extensions.Resilience or Microsoft.Extensions.Http.Resilience instead.
How to Use
To use Microsoft.Extensions.Http.Polly, follow these steps:
Installation
dotnet add package Microsoft.Extensions.Http.Polly
Usage
Handle transient faults
AddTransientHttpErrorPolicy can be used define a policy that handles transient errors:
builder.Services.AddHttpClient("PollyWaitAndRetry")
.AddTransientHttpErrorPolicy(policyBuilder =>
policyBuilder.WaitAndRetryAsync(
retryCount: 3,
retryNumber => TimeSpan.FromMilliseconds(600)));
In the preceding example, failed requests are retried up to three times with a delay of 600 ms between attempts.
Dynamically select policies
To dynamically inspect a request and decide which policy apply, use the AddPolicyHandler extension method:
var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(10));
var longTimeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(30));
builder.Services.AddHttpClient("PollyDynamic")
.AddPolicyHandler(httpRequestMessage =>
httpRequestMessage.Method == HttpMethod.Get
? timeoutPolicy
: longTimeoutPolicy);
In this example, if the outgoing request is an HTTP GET, a 10-second timeout is applied. For any other HTTP method, a 30-second timeout is used.
Main Types
The main types provided by this package are:
PollyHttpClientBuilderExtensions: Provides extension methods for configuringPolicyHttpMessageHandlermessage handlers as part of anHttpClientmessage handler pipelinePolicyHttpMessageHandler: ADelegatingHandlerimplementation that executes request processing surrounded by aPolly.PolicyPollyServiceCollectionExtensions: Provides convenience extension methods to registerPolly.Registry.IPolicyRegistry<string>andPolly.Registry.IReadOnlyPolicyRegistry<string>in a service collectionHttpRequestMessageExtensions: Provides extension methods forHttpRequestMessagePolly integration
Additional Documentation
For additional documentation and examples, refer to the official documentation on using Polly-based handlers in ASP.NET Core.
Feedback & Contributing
Microsoft.Extensions.Http.Polly is released as open-source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
Showing the top 20 packages that depend on Microsoft.Extensions.Http.Polly.
| Packages |
|---|
|
LynxChess.Lynx
Lynx chess engine
|
.NET Standard 2.0
- Microsoft.Extensions.Http (>= 10.0.0)
- Polly (>= 7.2.4)
- Polly.Extensions.Http (>= 3.0.0)