In my ASP.NET Core MVC site, I have a SignalR hub that has my DbContext
injected into the constructor. The hub pulls data from the database and send it to a kendo ui grid for the user to view. This data is filtered in that hub on which group is selected (stored in the database).
The group selection is done outside the context of the hub. When I change the users selected group the page reloads to update various UI elements. When the signalr hub is then called, the selected group is still set as what it was prior to the change. After digging for a bit I came across this issue on the signalr github. What I understand is because the hub is transient, the DbContext
is as well and since the hub is long running the DbContext
is never updated.
Is there a simple way around this while still dependency injecting the DbContext
or do I need to create and dispose a new context for every call? If so what is the best way to go about doing that and still pass the connection string from the appsettings.json
?
EDIT
I am currently using Microsoft.AspNetCore.SignalR.Server
and not the new Microsoft.AspNetCore.SignalR
library.
signalr core
scoped services and in particularDbContext
can be used like inasp.net core
controllers. See this answer – AlbertK