Saving persistence in SignalR Core hub

  • Thread starter Thread starter Fabs1977
  • Start date Start date
F

Fabs1977

Guest
I have a .Net Core 2.2 Web API for a chat app with a SignalR hub. I'd like to save the chats to the database (MySQL). At first I thought I could inject my DbContext into the hub via dependency injection. But then I read that the hub is Singleton whereas the DbContext (when used via the AddDbContext helper) is Scoped. I then read another post where it was suggested to force AddDbContext to add the context as singleton. But I've also read this is a really bad idea. Finally I read a post where someone suggested simply getting the DbContext from within my Hub by using DbContextFactory, and they provided this example:

using (var dbContextScope = dbContextScopeFactory.Create(options))
{
//do database stuff
dbContextScope.SaveChanges();
}


However, I don't know how to get an instance of DbContextScopeFactory in my hub. I tried injecting IDbContextScopeFactory into my hub's constructor, but it just says that the type or namespace IDbContextScopeFactory could not be found

Could anyone perhaps point me in the right direction?

Thanks


Fabricio Rodriguez - Pretoria, South Africa

Continue reading...
 
Back
Top