From a Page I'm calling a service like so:
_ = _importUserService.AddManyUserAsync(ValidatedUsers);
I want the Page() to return and let the user/UI move on while the service adds many users utilizing UserManager in the background. The problem is that as soon as the Page() returns and the request is finished, the IdentityDbContext is closed and UserManager no longer functions. How do I keep UserManager working after a request is finished?
The service is using standard dependency injection:
private readonly UserManager<ApplicationUser> _userManager;
public ImportUserService(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
It works fine if I use await instead of the discard and force the service to finish before the page is loaded; however, this is terrible UX if the user needs to wait 2 minutes for thousands of users to be imported before the page loads.. My question is without creating a stored procedure and replicating what UserManager is doing, is there a way to use UserManager in a service that is not dependent on the lifetime of a request?
My thoughts are to create a new instance of UserManager and not rely on the instance from the dependancy injection, but I have so far been unable to get that to work. Not sure what the syntax would be and cannot find an example that actually works in .net core 3.1
How do I keep IdentityDbContext alive after a request so that a service can complete its task using UserManager? (.Net Core 3.1)