ASP.NET Core 6: implementing Circuit Breaker Pattern Implementation in Resilience4j
I'm sure I'm missing something obvious here, but I'm refactoring my project and I'm collaborating on a project where I'm migrating some code and I'm converting an old project and I'm trying to implement the Circuit Breaker pattern using Resilience4j in my ASP.NET Core 6 application, but I'm working with issues with event notifications... Despite setting up the circuit breaker and the configuration, the notifications for state transitions aren't firing as expected. Here's what I've done so far: I created a service to handle external API calls with the following code: ```csharp public class ApiService { private readonly IExternalApiClient _apiClient; private readonly ICircuitBreaker _circuitBreaker; public ApiService(IExternalApiClient apiClient, ICircuitBreaker circuitBreaker) { _apiClient = apiClient; _circuitBreaker = circuitBreaker; } public async Task<string> GetDataAsync() { return await _circuitBreaker.ExecuteAsync(() => _apiClient.FetchDataAsync()); } } ``` In my `Startup.cs`, I configured Resilience4j like this: ```csharp services.AddResilience4j(options => { options.CircuitBreaker.Configure("api-service", config => { config.SetFailureRateThreshold(50); config.SetSlidingWindowSize(10); config.SetWaitDurationInOpenState(TimeSpan.FromSeconds(30)); }); }); ``` I expected to see notifications when the circuit state changes, but I’ve added the following event listeners, and none of them seem to trigger: ```csharp _circuitBreaker.OnStateTransition += (sender, args) => { Console.WriteLine($"Circuit state changed from {args.OldState} to {args.NewState}"); }; ``` However, I'm not seeing any output in the console when I deliberately cause failures in the external API. I even set up logging to ensure that the errors are being logged, and they are, but the circuit breaker doesn't seem to be responding as expected. I've also checked to make sure the `ICircuitBreaker` instance injected is the same one that was configured. Can someone guide to understand what I might be doing wrong? Are there specific configurations that need to be checked to ensure the circuit breaker functions correctly in an ASP.NET Core application? Any insights would be greatly appreciated! I'm working in a Debian environment. Has anyone dealt with something similar? This is happening in both development and production on macOS. Any feedback is welcome! I've been using C# for about a year now. Any ideas what could be causing this? I'm using C# 3.9 in this project. I'd really appreciate any guidance on this. I recently upgraded to C# latest. Any advice would be much appreciated. Is there a better approach? I'm working on a REST API that needs to handle this. What am I doing wrong?