Search code examples
microservicesasp.net-core-webapipollyresiliencyhttpclientfactory

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 are the differences between them?


Solution

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