-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
The documentation is not clear how to inject and work with a DBContext or scoped service in SignalR, which I believe would be a fairly common scenario. Hubs have a Transient
lifetime. According to the dependency injection documentation it says "Services that use the database context should also use the scoped lifetime". It's not clear how scoped services are handled in signalR.
I'm currently having an issue resulting in error A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
on my DBContext intermittently. I have a feeling this is related to the difference in my Scoped
DBContext and Transient
Hub. In a normal HTTP request it's clear that a "Scoped" service is created at the beginning of each http request. However in a websocket situation, when is a Scoped service created? Is the scoped service created when the socket establishes connection and the scope ends when the socket is disconnected? It seems like my problem is due to a Transient Hub using the same scoped service across multiple Hub method calls resulting in the threading error mentioned above.
Note: I've already checked for async method invocations that are missing await expressions using Regex and ReSharper, and have found none. I've never had this much of a problem on normal controllers so I'm led to believe this is an issue with how SignalR handles scoped services which isn't documented.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 1387f556-b4b7-70f9-5bd0-f0bccc597689
- Version Independent ID: fb016cdc-d281-6b5b-7466-2c718e7274b4
- Content: Use hubs in ASP.NET Core SignalR
- Content Source: aspnetcore/signalr/hubs.md
- Product: aspnet-core
- GitHub Login: @tdykstra
- Microsoft Alias: tdykstra