I am implementing a method that receives data from an API. At the moment, the API is adding the data to the database by using a loop. This however is looking more and more unreliable as the data keeps growing.
Since I am adding the data to two separate tables, I have been able to optimize the addition to one table by using AddRange but for the users, adding using UserManager has to go through a loop. To create one user at a time, resulting in prolonged load times.
I was thinking to create a service that runs in the background and send a list of users to be created to it in a fire and forget
var newEmployees = employees.Where(e => !string.IsNullOrEmpty(e.Email) &&
!existingEmails.Contains(e.Email)).ToList();
if (newEmployees.Any())
{
Task.Run(() =>
{
using var scope = _serviceProvider.CreateScope();
var userCreator = scope.ServiceProvider.GetRequiredService<ICreateUsers();
userCreator.CreateStaff(newEmployees, user.BusinessId).GetAwaiter().GetResult();
});
}
The problem with this is that I would not be able to get stats on which users were created and why they were not created in real time. I would have to depend on logs.
Does anyone have a better way to do this?
await Task.WhenAll(users.Select(u => user Manager.CreateUser(usually.Name, ...)))