Please correct me if I am wrong, unlike MVC/WASM, in Blazor Server, a scoped service is shared across all child components inside the same Razor component. A typical example is DbContext, which according the Microsoft's recommendation, should be created inside each component by a IDbContextFactory registered as Singleton by default in startup. Something like:
// demo.razor
@inject IDbContextFactory<MyDbContext> DbFactory
@implements IAsyncDisposable
@code {
private MyDbContext _context = default!;
protected override async Task OnInitializedAsync()
{
_context = await DbFactory.CreateDbContextAsync();
//do something...
await base.OnInitializedAsync();
}
async ValueTask IAsyncDisposable.DisposeAsync() {
if (_context is not null)
{
await _context.DisposeAsync();
}
}
Now comes the question that should UserManager be created and disposed the same way as DbContext? I understand now that UserManager has a public Dispose method, it should be disposed when we are done with it.
But if we call its Dispose method inside a Razor component, all other child components in the same component will not be able to access this UserManager any more, since the UserManager is the same instance within the scope.
I have not found something like IUserManagerFactory to create a new instance of UserManager for a component. Or just don't call UserManager.Dispose() and let the framework handle it?