Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Latest commit

 

History

History
28 lines (25 loc) · 1.16 KB

README.md

File metadata and controls

28 lines (25 loc) · 1.16 KB

Steps to include Polly in project using Curcuit Breaker pattern:

  1. Have your project grab the ASPNET Core 2.1 packages from nuget. You'll typically need the AspNetCore metapackage, and the extension package Microsoft.Extensions.Http.Polly.

  2. 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));
}
  1. 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