CodexBloom - Programming Q&A Platform

ASP.NET Core 6 Blazor Server: Handling Concurrent Requests with SignalR Issues

๐Ÿ‘€ Views: 1236 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-12
asp.net-core blazor signalr concurrency csharp

I've been working on this all day and I'm trying to implement I'm working on a personal project and I've been banging my head against this for hours... I'm working on an ASP.NET Core 6 Blazor Server application that uses SignalR for real-time notifications. I've set up a basic SignalR hub and Iโ€™m trying to broadcast messages to all connected clients whenever a specific event occurs. However, I'm encountering issues when multiple users trigger the event simultaneously. The notifications seem to be delayed, and sometimes they donโ€™t reach all clients at all. Here's a snippet of my SignalR hub: ```csharp public class NotificationHub : Hub { public async Task SendNotification(string message) { await Clients.All.SendAsync("ReceiveNotification", message); } } ``` And in my Blazor component, I call this hub method like this: ```csharp @inject IHubContext<NotificationHub> HubContext private async Task TriggerNotification(string message) { await HubContext.Clients.All.SendAsync("ReceiveNotification", message); } ``` I configured the SignalR service in `Startup.cs` like so: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddSignalR(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapHub<NotificationHub>("/notificationHub"); }); } ``` The Blazor component triggers the `TriggerNotification` method based on a user action, but when multiple users perform this action, I see this error in the console: ``` SignalR: Connection disconnected. Code: 1006. Reason: The connection was closed abnormally. ``` I've verified that the SignalR connection is established correctly at the start, and I can send messages successfully when thereโ€™s a single triggering user. I've also tried increasing the `KeepAliveInterval` in the SignalR options to 30 seconds, but this hasn't resolved the issue. I suspect it might be related to how Blazor handles state and concurrent requests in a server-side scenario. Any insights on how to effectively manage this scenario and ensure that all notifications are delivered promptly would be greatly appreciated. Are there any best practices or configurations that I might be missing? My development environment is Linux. Any help would be greatly appreciated! This is part of a larger application I'm building. This is for a REST API running on Debian. Any help would be greatly appreciated! I appreciate any insights!