Posts

Showing posts with the label Polly

Implement timeout and retry policies for HttpClient in ASP NET Core with Polly

Implement timeout and retry policies for HttpClient in ASP NET Core with Polly Few weeks ago I explained [how to use the new HttpClientFactory . This freed ourselves from managing the confusing lifecycle of a HttpClient and at the same time allowed us to setup commmon options like base address for all HttpClient injections in our classes. Today we will see how we can setup timeout and retry policies for the HttpClient in in ASP NET Core using Polly . Setup a simple service using HttpClient Install Polly extensions for ASP NET Core Setup Polly policies 1. Setup a simple service using HttpClient In the previous blog post we saw how we could setup a HttpClient and register it to the service collection. Here is a simple service client which will be injected throughout the application: public interface IMyApiClient { Task<HttpResponseMessage> Get(); } And here is the implementation: public class MyApiClient : IMyApiClient { private readonly HttpClient _client; ...