ASP.NET MVC 5 page hangs on db call

I am trying to get my MVC 5 project up and running using ASP.NET Identity 2.0. The starting point is this tutorial's sample application . My first page is Home / Index. When I try to do a search (expecting a zero return value) the application just hangs and I can't figure out why. The instant action of the db context makes a call to the Seed () method, which seems to be fine, but then it hangs on the call to roleManager.FindByName (roleName) (I commented it out in the code below). The debugger pause showed where it got stuck, but I don't know where to go from there. The relevant classes are as follows.

Controller:

public class HomeController : ApplicationController
{         
    public ActionResult Index()
    {
        var user = db.Users.Find("dummyVal");

        return View();
    }

    [Authorize]
    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

      

Basic controller:

public abstract class ApplicationController : Controller
{
    protected ApplicationDbContext db;

    public ApplicationController()
    {
        db = new ApplicationDbContext();
    }
}

      

DB initializer:

public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext> 
{
    protected override void Seed(ApplicationDbContext context) {
        InitializeIdentityForEF(context);
        base.Seed(context);
    }

    //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
    public static void InitializeIdentityForEF(ApplicationDbContext db) {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
        const string name = "admin@example.com";
        const string password = "Admin@123456";
        const string roleName = "Admin";

        //Create Role Admin if it does not exist
        // EXECUTION HANGS ON THIS CALL...
        var role = roleManager.FindByName(roleName);
        if (role == null) {
            role = new IdentityRole(roleName);
            var roleresult = roleManager.Create(role);
        }

        var user = userManager.FindByName(name);
        if (user == null) {
            user = new ApplicationUser { UserName = name, Email = name };
            var result = userManager.Create(user, password);
            result = userManager.SetLockoutEnabled(user.Id, false);
        }

        // Add user admin to Role Admin if not already added
        var rolesForUser = userManager.GetRoles(user.Id);
        if (!rolesForUser.Contains(role.Name)) {
            var result = userManager.AddToRole(user.Id, role.Name);
        }

        // Create dummy user
        const string dummyUserName = "PeterVenkman";
        const string dummyUserPwd = "gf%^^ftf83X";

        var dummyUser = userManager.FindByName(dummyUserName);
        if (dummyUser == null) { 
            dummyUser = new ApplicationUser { UserName = dummyUserName, Email = dummyUserName };
            var result = userManager.Create(dummyUser, dummyUserPwd);
            result = userManager.SetLockoutEnabled(dummyUser.Id, false);
        }
    }
}

      

+3


source to share


3 answers


The error is due to the fact that you are creating a dbContext object in the controller and not getting it from the owin context. Having two different context objects, one in the controller and one in startup, running on the same db, causes this deadlock. The easiest way to resolve this is to replace the code

            db = new ApplicationDbContext();

      

from



db = System.Web.HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>();

      

fixes the problem

+13


source


I had the same problem and fixed it.

  • Delete current database (I used .mdf)
  • Create a new database
  • Clean up and restore
  • Update the database - if necessary -

Unfortunately, I don't know the source of the problem. Please edit the answer / post a comment if you know :)



For other people with the same problem, make sure the username / password in ApplicationDbInitializer is valid. It will hang if you set the admin password to 123 for example.

Edit: This post has an explanation + answer http://forums.asp.net/post/5802779.aspx

+4


source


Suhas Joshi has the correct answer. The ApplicationDbContext object must be essentially a singleton managed by the Microsoft ASPNET Identity Owin package (installed using NuGet). When you manually create a new instance of this application in your ApplicationController, there is contention for the same resource that is causing this deadlock.

Note that the "Get ()" extension is used from the following library, which you must reference in your ApplicationContext class.

using Microsoft.AspNet.Identity.Owin; 

      

Thanks to Suhas Joshi for saying this, as he saved me so much. I would just vote your answer, but unfortunately I don't have enough reputation yet :).

+4


source







All Articles