Repository navigation
Conversation
…ation The default IHubActivator<THub> is registered as scoped, so every hub invocation (and OnConnected/OnDisconnected) resolved it from a new scope, which populates the scope's resolved-services storage. Once the resolved activator is known to be the internal DefaultHubActivator<THub>, which can only come from the default registration, create it directly over the scoped service provider instead. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The singleton dispatcher can permanently bypass a scoped activator factory after resolving one default instance, causing later scopes to ignore custom activator creation and release behavior.
What changed in this PR
This PR reduces SignalR hub activation overhead by constructing the default activator directly after detecting it, avoiding repeated scoped DI resolution.
Changes:
- Routes hub invocations and lifecycle callbacks through a shared activator-selection helper.
- Adds tests for hub disposal and DI-owned hub lifetime behavior.
- Adds a disposal-count method to the existing test hub.
| File | Description |
|---|---|
| src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTestUtils/Hubs.cs | Exposes disposal counts for test assertions. |
| src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.cs | Adds activation and disposal lifecycle tests. |
| src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs | Adds the default-activator fast path for invocations and lifecycle callbacks. |
Review effort: Balanced
… unchanged Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
Pushed an empty commit (bca790c) to retrigger CI. The only failure was the Blazor E2E test 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Superseded by #70141, which contains the same change. Closing this duplicate is left to maintainers. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |

Avoid resolving the default hub activator from DI for every hub invocation
Cut the per-invocation DI cost of creating hubs with the default activator.
Description
IHubActivator<>is registered as scoped (TryAddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>))).DefaultHubDispatchercreates a new scope for every hub invocation (and forOnConnectedAsync,OnDisconnectedAsyncandOnAuthenticationRefreshedAsync) and resolves the activator from it. That scoped resolution is often the only thing that populates the scope's resolved-services dictionary, so each invocation also paid for the dictionary's buckets and entries, plus the DI lookup.The dispatcher now resolves the activator through DI the first time, as before. It switches to constructing
new DefaultHubActivator<THub>(scope.ServiceProvider)directly, which is the same object DI would create, only when two things hold:DefaultHubActivator<THub>, andAddSignalRCoreregisters an internalHubActivatorRegistrationssingleton that's built after the container and reads the non-keyedIHubActivator<>descriptors: the last open-generic one must beDefaultHubActivator<>, and there must be no closedIHubActivator<THub>registration.Custom activators, factories (including ones that sometimes return the default), decorators and per-hub overrides keep being resolved from DI on every call.
Hub creation and release are unchanged.
DefaultHubActivatorstill resolvesTHubfrom the scoped provider when it's registered, or creates it withActivatorUtilitiesand disposes it on release. No public API changes.When hub constructors resolve other scoped services, the scope's dictionary is allocated anyway. The saving in that case is just the DI lookup for the activator.
Benchmark
DefaultHubDispatcherBenchmark(existing), BenchmarkDotNet InProcess with MemoryDiagnoser,--warmupCount 3 --iterationCount 15. 2 vCPU Linux VM, .NET 12 (repo SDK), Release. Rounds were interleaved: base, PR, base, PR.Every invocation path allocates 184 B less. The remaining stream and upload benchmarks involve thread hops, so their timings are too noisy on this VM to call either way; their allocations also drop by about 184 B. An allocation-tick breakdown of
Invocationconfirms that the scope'sEntry[]andint[]buckets are no longer allocated.Tests
Microsoft.AspNetCore.SignalR.Tests: 500 passed, 1 skipped (pre-existing). Four new tests:HubConnectionHandlerTests.DefaultHubActivatorCreatesAndDisposesHubForEachInvocation: after the fast path is enabled, each invocation still gets a fresh hub that is disposed.HubConnectionHandlerTests.DefaultHubActivatorDoesNotDisposeHubResolvedFromServiceProvider: a hub registered in DI is still resolved from DI and never disposed by the activator.HubConnectionHandlerTests.ScopedHubActivatorFactoryIsResolvedForEveryScopeEvenIfItReturnsTheDefaultActivator: a scoped factory that returns the default activator in some scopes is still resolved for every scope.HubConnectionHandlerTests.HubActivatorRegistrationsOnlyReportsTheUnchangedDefaultRegistration: keyed registrations, closed per-hub registrations and later open-generic registrations are handled.IHubActivatortests (singleton and scoped registrations) pass unchanged.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)