0

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?

2
  • You can try await Task.WhenAll(users.Select(u => user Manager.CreateUser(usually.Name, ...))) Commented May 7 at 16:22
  • This works I had to use semaphore to process in batches. Thanks Commented May 9 at 5:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.