Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between AddTransientHttpErrorPolicy and AddPolicyHandler?

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?

like image 598
Dawid Avatar asked Jun 27 '18 10:06

Dawid


People also ask

What is IHttpClientFactory?

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.

What is AddHttpClient?

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.

Why use Polly?

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.

What is Polly circuit breaker?

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.


1 Answers

.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.

like image 103
mountain traveller Avatar answered Sep 28 '22 05:09

mountain traveller