Have your project grab the
ASPNET Core 2.1packages fromnuget. You'll typically need theAspNetCoremetapackage, and the extension packageMicrosoft.Extensions.Http.Polly.Configure a client with Polly policies, in
Startup. Insert this two private methods:
privatestaticIAsyncPolicy<HttpResponseMessage>GetRetryPolicy(){returnHttpPolicyExtensions.HandleTransientHttpError().OrResult(msg =>msg.StatusCode==System.Net.HttpStatusCode.NotFound).WaitAndRetryAsync(6, retryAttempt =>TimeSpan.FromSeconds(Math.Pow(2,retryAttempt)));}privatestaticIAsyncPolicy<HttpResponseMessage>GetCircuitBreakerPolicy(){returnHttpPolicyExtensions.HandleTransientHttpError().CircuitBreakerAsync(5,TimeSpan.FromSeconds(30));}- In your standard
Startup.ConfigureServices(...)method, start by configuring a named client as below:
services.AddHttpClient("ProfileHttpClient").AddPolicyHandler(GetRetryPolicy()).AddPolicyHandler(GetCircuitBreakerPolicy());whereProfileHttpClient-your HTTP client implementation