Polly.CircuitBreaker.BrokenCircuitException

winanjaya 41 Reputation points
2023-01-13T00:28:23.01+00:00

Hi,

I expect to have an automatic retry before the exception thrown, but why I still getting below exception in my Polly Breaker:

System.AggregateException: One or more errors occurred. (The circuit is now open and is not allowing calls.) ---> Polly.CircuitBreaker.BrokenCircuitException1[System.Net.Http.HttpResponseMessage]: The circuit is now open and is not allowing calls. at Polly.CircuitBreaker.CircuitStateController1.OnActionPreExecute() at Polly.CircuitBreaker.AsyncCircuitBreakerEngine.ImplementationAsyncTResult at Polly.AsyncPolicy1.ExecuteAsync(Func3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext) at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendCoreAsync(HttpRequestMessage request, Context context, CancellationToken cancellationToken) at Polly.Retry.AsyncRetryEngine.ImplementationAsyncTResult at Polly.AsyncPolicy1.ExecuteAsync(Func3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext) at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.g__Core|5_0(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)

my Polly Policies:

Retry Policies:


internal static AsyncRetryPolicy<HttpResponseMessage> GetRetryPolicy()

        {

            return HttpPolicyExtensions

            .HandleTransientHttpError()

            .OrResult(msg => (int)msg.StatusCode > (int)System.Net.HttpStatusCode.InternalServerError ||

            msg.StatusCode == HttpStatusCode.RequestTimeout)

            .Or<BrokenCircuitException>()

            .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)));

        }

Circuit Breaker Policies


internal static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
      {
            return HttpPolicyExtensions
                .HandleTransientHttpError()
                .Or<BrokenCircuitException>()
                .CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
        }

program.cs


builder.Services.AddSingleton<IM2M, M2M>();
builder.Services.AddHttpClient<IM2M, M2M>("m2m", c1 =>
{
    c1.BaseAddress = new Uri("https://m2m.example.com/api");
    c1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    c1.Timeout = TimeSpan.FromMinutes(30);
})
    .SetHandlerLifetime(TimeSpan.FromMinutes(30))
    .AddPolicyHandler(PollyPolicies.GetRetryPolicy())
    .AddPolicyHandler(PollyPolicies.GetCircuitBreakerPolicy())
    .ConfigureHttpClient((c2) =>
        new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = (sender, cert, chain,
            sslPolicyErrors) =>
            {
                return sslPolicyErrors == SslPolicyErrors.None;
            }
        }
    );

I need advice.

many thanks in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,606 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,031 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
619 questions
{count} votes