I want to apply resiliency strategy using Polly. I am using HttpClientFactory from ASP.NET Core 2.1. I found some guide on Polly GitHub wiki. There are two ways of such policy configuration - using AddTransientHttpErrorPolicy and AddPolicyHandler, but not much of an explanation. What is the difference between them?
NET Core 2.1 introduced two approaches, one of them being IHttpClientFactory. It's an interface that's used to configure and create HttpClient instances in an app through Dependency Injection (DI). It also provides extensions for Polly-based middleware to take advantage of delegating handlers in HttpClient.
AddHttpClient(IServiceCollection) Adds the IHttpClientFactory and related services to the IServiceCollection. AddHttpClient(IServiceCollection, String) Adds the IHttpClientFactory and related services to the IServiceCollection and configures a named HttpClient.
Polly is a . NET library that provides resilience and transient-fault handling capabilities. You can implement those capabilities by applying Polly policies such as Retry, Circuit Breaker, Bulkhead Isolation, Timeout, and Fallback.
Polly is a . NET resilience and transient-fault-handling library that allows developers to express policies such as retry, circuit breaker, timeout, bulkhead isolation, and so forth. It is a mature library that is almost synonymous with app resiliency.
.AddTransientHttpErrorPolicy(...)
embeds a specification for you of the what to handle (network failures, 5xx and 408 responses as described in the wiki). You only have to specify the how to handle (eg retry, circuit-breaker).
With .AddPolicyHandler(...)
, you specify the whole policy yourself: both what to handle (.Handle<>()
, .Or<>()
, .OrResult<HttpResponseMessage()
etc) and how to handle (eg retry, circuit-breaker). As shown here in the Polly wiki.
Beyond that, there are no differences in how IHttpClientFactory
works with the configured policies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With