-
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:
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
}
private static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
{
return HttpPolicyExtensions
.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());
where ProfileHttpClient - your HTTP client implementation