I am building a Blazor Server front end to an existing domain layer. This layer offers various injectable services to do modifications on the EF Core repository. For this, the services itself requests a DbContext from the (standard Microsoft) DI container. This works fine with regular MVC.NET/Razor pages with a scoped DbContext instance, but as documented, this is problematic with Blazor. In a Blazor Server app we’d want to use DbContextFactory to generate short-lived DbContext instances for operations instead.
It’s no problem to have both a DbContext and DbContextFactory in the same application, but I’m struggling to understand how to adapt my services. Or if I even need to? To illustrate, this is the current code:
My Page:
@page “/items”
@inject ItemService ItemService
// somewhere in the code
ItemService.DoOperation(…)
My service
class ItemService
{
public ItemService(MyDbContext myDbContext)
{
…
}
public bool DoOperation(…)
{
…
_myDbContext.SaveChanges();
}
}
Startup.cs:
services.AddDbContext<MyDbContext>(options => …),
contextLifetime: ServiceLifetime.Transient,
optionsLifetime: ServiceLifetime.Singleton
);
services.AddDbContextFactory<MyDbContext>(options => …);
I’ve changed the lifetimes for DbContext according to the example given in this answer and so far I haven’t been able to create any issues, but I don’t fully understand the lifetime issues at play here. How can I engineer my service to play well in both a Blazor and a MVC/Razor Pages application in an obvious way?