Skip to content

ServiceConnectionManager<T> throws NullReferenceException on DisposeAsync() if SetServiceConnection(...) was never called #2190

@alejandropadillav1

Description

@alejandropadillav1

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

  1. Configure SignalR via:

builder.Services.AddSignalR().AddAzureSignalR(options => { options.ConnectionString = config["Azure:SignalR:ConnectionString"]; });

  1. 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.

  1. Host shutdown triggers, DisposeAsync() is called on all services.

  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions