I am trying to build a minimal working example of a CircuitHandler in a Blazor server side application but none of the CircuitHandler methods are called. I feel like I'm missing something obvious.
I'm developing on macOS Sonoma 14.5 using the .NET SDK Arm64 version 8.0.7.
I have used the dotnet commandline tool (version 8.0.303) to create a new project:
dotnet new blazor -o CircuitHandlerAppIn file MyCircuitHandler.cs I have added a simple CircuitHandler implementation:
using Microsoft.AspNetCore.Components.Server.Circuits;namespace CircuitHandlerApp;public class MyCircuitHandler : CircuitHandler{ public MyCircuitHandler() { Console.WriteLine("Constructor called."); } public override Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken) { Console.WriteLine("OnCircuitOpenedAsync called."); return base.OnCircuitOpenedAsync(circuit, cancellationToken); } public override Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken) { Console.WriteLine("OnConnectionUpAsync called."); return Task.CompletedTask; } public override Task OnConnectionDownAsync(Circuit circuit, CancellationToken cancellationToken) { Console.WriteLine("OnConnectionDownAsync called."); return Task.CompletedTask; } public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken) { Console.WriteLine("OnCircuitClosedAsync called."); return base.OnCircuitClosedAsync(circuit, cancellationToken); }}I have registered the handler in Program.cs like this:
...builder.Services.AddRazorComponents() .AddInteractiveServerComponents();builder.Services.AddSingleton<CircuitHandler,MyCircuitHandler>();var app = builder.Build();...In my interpretation of the documentation and other sources that should be enough to see some of the CircuitHandler methods being called. But that does not happen when I run the application and connect to it in the browser.
Also the constructor of MyCircuitHandler only gets called when I directly inject the CircuitHandler into one of the components.
On Github I didn't find any issues that sound similar to this problem and I would think that this feature is too essential that no one would have found a bug here before me. So it is probably more a configuration problem.
I have tried using the .NET 9 Preview but that didn't work either.
Here's the full Program.cs file:
using CircuitHandlerTest;using CircuitHandlerTest.Components;using Microsoft.AspNetCore.Components.Server.Circuits;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorComponents() .AddInteractiveServerComponents(options => { options.DetailedErrors = true; });builder.Services.AddSingleton<CircuitHandler, MyCircuitHandler>();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){ app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();app.Run();