-
Notifications
You must be signed in to change notification settings - Fork 108
Description
Describe the bug
When using AddAzureSignalR() in an ASP.NET Core app, if another service fails during startup (e.g., due to invalid configuration and such), the app may throw before reaching app.Run() — and before SignalR is fully initialized.
In this case, ServiceConnectionManager never calls SetServiceConnection(...).
However, during host shutdown, SignalR still attempts to dispose the uninitialized ServiceConnectionManager, resulting in an unhandled NullReferenceException.
To Reproduce
- Configure SignalR via:
builder.Services.AddSignalR().AddAzureSignalR(options => { options.ConnectionString = config["Azure:SignalR:ConnectionString"]; });
- Register a dependent service with invalid config:
services.AddSingleton<MyService>(provider => { var baseUrl = config["MyService:BaseUrl"]; if (string.IsNullOrEmpty(baseUrl)) throw new ArgumentException("BaseUrl cannot be empty"); return new MyService(baseUrl); });
3. The app fails before app.Run() due to ArgumentException.
-
Host shutdown triggers, DisposeAsync() is called on all services.
-
SignalR’s internal ServiceConnectionManager.StopAsync() fails due to _serviceConnection being null.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Azure.SignalR.ServiceConnectionManager1.StopAsync() at Microsoft.Azure.SignalR.ServiceConnectionManager1.Dispose()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.DisposeAsync()
Exceptions (if any)
-
DisposeAsync() should guard against uninitialized internal state.
-
If SetServiceConnection(...) was never called, StopAsync() should be skipped or handled gracefully.
-
Alternatively, provide a public flag or lifecycle hook to safely detect incomplete initialization.
Further technical details
- Azure SignalR SDK version latest
- Your Server ASPNETCORE version or Assembly version of
Microsoft.AspNetCore.SignalR: 8.0