代码之家  ›  专栏  ›  技术社区  ›  Rutger

向用户添加角色,获取SQL超时

  •  0
  • Rutger  · 技术社区  · 8 年前

    在我的应用程序(MVC/WebAPI,使用Owin)中,我使用localDB设置了集成测试。在我的一个测试中,我似乎能够从UserManager查询数据,但我无法更新任何数据。

    ApplicationUserManager安装程序:

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
        {
            UserValidator = new UserValidator<ApplicationUser>(this)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
    
            PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };
    
            UserLockoutEnabledByDefault = true;
            DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            MaxFailedAccessAttemptsBeforeLockout = 5;
        }
    }
    

    Unity设置:

    container
        .RegisterType<DbContext, ApplicationDbContext>()
        .RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>()
        .RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication))
        .RegisterType<IRoleStore<IdentityRole, string>, RoleStore<IdentityRole, string, IdentityUserRole>>()
        .RegisterType<ApplicationUserManager>()
        .RegisterType<ApplicationSignInManager>()
        .RegisterType<ApplicationRoleManager>()
    

    正在测试的代码:

    //Working
    var user = await _applicationUserManager.FindByIdAsync(userId);
    //SQL timeout
    identityResult = await _applicationUserManager.AddToRoleAsync(user.Id, Roles.User);
    

    我尝试过其他更新(比如更新用户名),但似乎都不起作用。当我在使用网站时调用相同的功能时(或在使用Postman的API调用中),一切都正常。

    有什么建议吗?

    更新1:

    当我将sql超时设置提高到180秒时,我能够通过第一次usermanager更新(在等待大约60秒后)。以下任何对数据库的调用都会失败,提供程序在打开时失败。

    更新2:

    我用同样的方法测试了localdb性能:

    for (int i = 0; i < 1000; i++)
    {
        _test.LogError(new System.Exception(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
    }
    

    此操作(1000个具有不同数据的插入)大约需要1秒才能完成。

    更新3:

    更新用户后暂停测试时,我无法再访问SSMS中的数据库。它似乎处于锁定状态。

    更新4:

    出于测试目的,我自己在usermanager中编写了更新代码。我不再得到超时,但它似乎保持连接或事务打开。在运行以下代码时,我似乎无法再从SSMS中的用户表中选择任何内容。

    var userToUpgrade = _context.Users.Single(user => user.Id == userId);
    
    foreach (var role in userToUpgrade.Roles.ToList())
    {
        userToUpgrade.Roles.Remove(role);
    }
    
    userToUpgrade.Roles.Add(new IdentityUserRole
    {
        UserId = userId,
        RoleId = _context.Roles.Single(role => role.Name == Roles.TestUser).Id
    });
    
    _context.Entry(userToUpgrade).State = EntityState.Modified;
    _context.SaveChanges();
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Rutger    8 年前

    那太愚蠢了。

    更改了以下代码:

    internal void StartTestScope()
    {
        _transactionScope = new TransactionScope();
    }
    

    收件人:

    internal void StartTestScope()
    {
        TransactionOptions options = new TransactionOptions();
        options.IsolationLevel = IsolationLevel.ReadUncommitted;
        _transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, options);
    }
    

    在开始集成测试之前,我会启动一个新的范围/事务。运行测试后,我将其拆掉。usermanager在内部启动了自己的事务,导致我无法读取更改。

    谢谢你的建议,帮我找到了正确的方向!

    推荐文章