asp.net - Register users synchronously during context seed -


in asp.net mvc 5 project, have custom initializer class:

class custominitializer : dropcreatedatabaseifmodelchanges<ghazanetcontext> 

i want seed context several users. problem new mvc 5 async , whatever try fails. here's code attempts create user:

    private void addrestaurant(registerrestaurantviewmodel model, dbcontext db)     {         user user = new user(model.username) { addresses = new list<address>(), role = "restaurant" };         user.addresses.add(model.address);          task.factory.startnew(async () =>             {                 await users.create(user); // gets stuck here                  var newuser = db.users.where(u => u.username == user.username).firstordefault();                 var restaurant = new restaurant(model) { user = newuser };                 db.restaurants.add(restaurant);                 db.savechanges();                 await secrets.create(new usersecret(model.username, model.password));                 await logins.add(new userlogin(user.id, identityconfig.localloginprovider, model.username));                  const string role = "restaurant";                 if (!await roles.roleexists(role))                     await roles.createrole(new role(role));                  await roles.addusertorole(role, user.id);             }).runsynchronously();     } } 

when run code, gets stuck in line above when run in website , register users, works perfectly. there i'm missing here?

you're using task.factory.startnew , executesynchronously, neither of should used in async code.

the best way invoke async code invoke from async code, such:

private async task addrestaurantasync(registerrestaurantviewmodel model, dbcontext db) {     user user = new user(model.username) { addresses = new list<address>(), role = "restaurant" };     user.addresses.add(model.address);     await users.create(user);     var newuser = db.users.where(u => u.username == user.username).firstordefault();     var restaurant = new restaurant(model) { user = newuser };     db.restaurants.add(restaurant);     db.savechanges();     await secrets.create(new usersecret(model.username, model.password));     await logins.add(new userlogin(user.id, identityconfig.localloginprovider, model.username));      const string role = "restaurant";     if (!await roles.roleexists(role))         await roles.createrole(new role(role));      await roles.addusertorole(role, user.id); } 

this have cascading effect through code base (the callers of addrestaurantasync must async, etc), it's easiest , cleanest way of calling async code. various hacks exist calling asynchronous code in synchronous manner, it's easy deadlock situations , if right impact scalability of site.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -