Hello,
Here are a couple of tips that will hopefully help you 😀
CreateAsync
in parallel the UserManager cannot use the same DbContext instance. A new context can be provided when instantiating the UserManager, for example:var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
The main point is that in every thread the user manager should to use a new DbContext, preventing issues such as 'A second operation was started'. It should be possible to do this using dependency injection as well.Hi ynze, thanks for the response
currently I am getting the user manager in scheduled job like this.
var userManager = ServiceLocator.Current.GetInstance<ApplicationUserManager<SiteUser>>();
when I changed this to
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
I am facing two issues
At UserManager: There is no argument given that corresponds to the required formal parameter 'optionsAccessor' of 'UserManager<ApplicationUser>.UserManager(IUserStore<ApplicationUser>, IOptions<IdentityOptions>, IPasswordHasher<ApplicationUser>, IEnumerable<IUserValidator<ApplicationUser>>, IEnumerable<IPasswordValidator<ApplicationUser>>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger<UserManager<ApplicationUser>>)
At ApplicationDbContext: I got the following errors
1. Using the generic type 'ApplicationDbContext<TUser>' requires 1 type arguments.
There is no argument given that corresponds to the required formal parameter 'options' of 'ApplicationDbContext<ApplicationUser>.ApplicationDbContext(DbContextOptions<ApplicationDbContext<ApplicationUser>>)
Currently modified as below but still facing issue with UserManager regarding 'optionsAccessor' parameter
DbContextOptions<ApplicationDbContext<ApplicationUser>> options = new();
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext<ApplicationUser>(options)));
Can you please help me with these parameters which are required in the instantiation of the user manager
why do you need to run in parallel? If is 100K users one time I would just create a schedule job and do synchronously
As a rule of thumb, always try to max out single thread performance before thinking about running in parallel. DotTrace is a great tool to find bottleneck and optimize .
I am handling this migration in scheduled job only. The issue with importing users synchronously is that the process is slowing down with the number of migrated users. It completed only 16000 users in 4hrs and I am trying to speed up this process.
As I said above, try dotTrace to see if you can find the bottlenecks in your code
Praveen. I've just written this quick snippet. Try this and amend to your own needs if it works. Remember to call wait() on your entire method of logic inside the Execute() of the schedule job if you want the job not to finish before your async methods do.
foreach (var user in userList)
{
// Create new dbcontext for every user
var _configuration = ServiceLocator.Current.GetInstance<IConfiguration>();
var options = new DbContextOptionsBuilder<ApplicationDbContext<SiteUser>>().UseSqlServer(_configuration.GetConnectionString("EcfSqlConnection")).Options;
using (var dbContext = new ApplicationDbContext<SiteUser>(options))
{
var store = new UserStore<SiteUser>(dbContext);
var result = Task.Run(() => store.CreateAsync(user)).Result;
}
}
Hi, We are trying to create around 100k users in Optimizely programmatically but the issue is can't able to run _userManager.CreateAsync method parallelly as we are facing the following errors with multiple threads.
1. System.InvalidOperationException: An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside 'OnConfiguring' since it is still being configured at this point. This can happen if a second operation is started on this context before a previous operation completed. Any instance members are not guaranteed to be thread-safe.
2. A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext.
and when to run in series the process is becoming slow with the increase in the number of users migrated and I can't able dispose and instantiate ApplicationUserManager due to issues with DBContext. Can somebody please help me with possible ways of migrating a large number of users in to optimizely?