ASP.NET MVC 5页挂在DB调用 [英] ASP.NET MVC 5 page hangs on db call

查看:181
本文介绍了ASP.NET MVC 5页挂在DB调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图站起来,用ASP.NET 2.0的身份一个MVC 5项目运行。我的出发点是在<一个的示例应用程序href=\"http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity#build\"相对=nofollow>本教程。我最初的页面首页/索引。当我尝试做一个搜索(期待一个空的返回值)的应用程序只需挂,我想不通为什么。数据库上下文的实例化是导致种子()方法被调用,这似乎是正常的,但后来挂在roleManager.FindByName(角色名)调用(我在下面的code评论它)。暂停调试器显示的地方被卡住了,我不知道从哪里里去。相关类如下。

控制器:

 公共类HomeController的:ApplicationController中
{
    公众的ActionResult指数()
    {
        变种用户= db.Users.Find(dummyVal);        返回查看();
    }    [授权]
    公众的ActionResult关于()
    {
        ViewBag.Message =你的应用描述页面。        返回查看();
    }    公众的ActionResult联系()
    {
        ViewBag.Message =您的联系页面。        返回查看();
    }
}

基本控制器:

 公共抽象类的ApplicationController:控制器
{
    保护ApplicationDbContext分贝;    公众的ApplicationController()
    {
        DB =新ApplicationDbContext();
    }
}

DB初始化程序:

 公共类ApplicationDbInitializer:DropCreateDatabaseAlways&LT; ApplicationDbContext&GT;
{
    保护覆盖无效的种子(ApplicationDbContext上下文){
        InitializeIdentityForEF(上下文);
        base.Seed(上下文);
    }    //使用密码@ 123456在管理角色创建User=Admin@Admin.com =管理员
    公共静态无效InitializeIdentityForEF(ApplicationDbContext DB){
        。VAR的UserManager = HttpContext.Current.GetOwinContext()GetUserManager&LT; ApplicationUserManager&GT;();
        。VAR roleManager = HttpContext.Current.GetOwinContext()获取&LT; ApplicationRoleManager&GT;();
        常量字符串名称=admin@example.com;
        常量字符串密码=管理@ 123456;
        常量字符串角色名=管理;        //创建角色admin,如果它不存在
        //挂起的执行在此调用...
        VAR角色= roleManager.FindByName(角色名);
        如果(角色== NULL){
            角色=新IdentityRole(角色名);
            VAR roleresult = roleManager.Create(作用);
        }        VAR用户= userManager.FindByName(名);
        如果(用户== NULL){
            用户=新ApplicationUser {用户名=名称,电子邮件=名};
            VAR的结果= userManager.Create(用户名,密码);
            结果= userManager.SetLockoutEnabled(user.Id,FALSE);
        }        // admin用户添加到管理员角色,如果尚未添加
        VAR rolesForUser = userManager.GetRoles(user.Id);
        如果(!rolesForUser.Contains(role.Name)){
            VAR的结果= userManager.AddToRole(user.Id,role.Name);
        }        //创建虚拟用户
        常量字符串dummyUserName =PeterVenkman;
        常量字符串dummyUserPwd =广发^^ ftf83X        VAR dummyUser = userManager.FindByName(dummyUserName);
        如果(dummyUser == NULL){
            dummyUser =新ApplicationUser {用户名= dummyUserName,电子邮件= dummyUserName};
            VAR的结果= userManager.Create(dummyUser,dummyUserPwd);
            结果= userManager.SetLockoutEnabled(dummyUser.Id,FALSE);
        }
    }
}


解决方案

该错误是因为你在实例化控制器中的DbContext对象,而不是从owin背景下得到它。两种不同语境的presence对象一个在控制器和一个在启动对同一数据库的工作造成这一僵局。解决这个简单的方法就是更换code

  DB =新ApplicationDbContext();

  DB = System.Web.HttpContext.Current.GetOwinContext()获取&LT; ApplicationDbContext&GT;();

修复该问题。

I'm trying to get up and running with an MVC 5 project using ASP.NET Identity 2.0. My starting point is the example app in this tutorial. My initial page is Home/Index. When I try to do a search (expecting a null return value) the app simply hangs and I can't figure out why. Instantiation of the db context is causing the Seed() method to be called, which seems normal, but then it hangs on the roleManager.FindByName(roleName) call (I've commented it in the code below). Pausing the debugger showed where it was stuck but I don't know where to go from there. 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();
    }
}

Base 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);
        }
    }
}

解决方案

The error is because you are instantiating the dbContext object in the controller and not getting it from the owin context. The presence of two different context objects one in the controller and one in the startup working on the same db is causing this deadlock. Simplest way to resolve this is replace the code

            db = new ApplicationDbContext();

with

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

fixes the issue

这篇关于ASP.NET MVC 5页挂在DB调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆