locked
A second operation started on this context before a previous asynchronous operation completed RRS feed

  • Question

  • User-570626059 posted

    Hi guys I am getting the below exception. Can you help please

    System.NotSupportedException: 'A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe

     
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                    IdentityResult result = manager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text);
                    
    
    
                    if (result.Succeeded)
                    {
    
    
                        var umanager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
    
                       var user = manager.FindById(User.Identity.GetUserId());
                       user.firstLogin = "N";
                       manager.UpdateAsync(user);
                        ///// issue here with context already in use
                    
                       
                        signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
                        Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
    
                        Global.GlobalVariables.firstTimeLogin = 0;
    
                    }
                    else
                    {
                        AddErrors(result);
                    }

    Tuesday, July 3, 2018 10:53 PM

Answers

  • User283571144 posted

    Hi skyblue28,

    According to your codes, I have created a test demo on my side, it works well.

    My test codes:

            protected void Button1_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                IdentityResult result = manager.ChangePassword(User.Identity.GetUserId(), currentpassword, Password.Text);
    
                if (result.Succeeded)
                {
    
    
                    var umanager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
    
                    var user = manager.FindById(User.Identity.GetUserId());
                    user.PhoneNumber = "1111111";
                     var re = manager.UpdateAsync(user).Result;
                    ///// issue here with context already in use
    
    
                    signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
                    Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
    
                    //Global.GlobalVariables.firstTimeLogin = 0;
    
                }
                else
                {
                    //AddErrors(result);
                }
            }

    Result:

    Besides, according to the error message, it measn there is only one asynchronous operation per context allowed at a time.

    So, you either have to await them one at a time as the error message suggests:

    For example:

    var banner = await context.Banners.ToListAsync();
    var re = await manager.UpdateAsync(user);

    Best Regards.

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2018 3:02 AM