我的角色提供程序看起来像下面的类(删节)。我在VS 2012上运行IIS Express和SQL 2012 Express,我在此角色提供程序代码中获得了大量看似随机的异常。
public class Educ8RoleProvider : RoleProvider
{
private readonly Educ8DbContext _dbContext = new Educ8DbContext();
private readonly Authorization _authorization;
public Educ8RoleProvider()
{
_authorization = new Authorization(_dbContext);
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
try
{
base.Initialize(name, config);
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
throw;
}
}
public override bool IsUserInRole(string username, string roleName)
{
return GetRolesForUser(username).Any(r => r.ToLower() == roleName);
}
public override string[] GetRolesForUser(string username)
{
return _authorization.GetRolesForMember(username).Select(r => r.Name).ToArray();
}
}
以下是我的Authorization
课程的示例:
public class Authorization
{
private readonly Educ8DbContext _dbContext;
private readonly IMemberRepository _memberRepository;
private readonly IRoleRepository _roleRepository;
private readonly IMemberRoleRepository _memberRoleRepository;
public Authorization(Educ8DbContext context)
{
_dbContext = context;
_memberRepository = new MemberRepository(_dbContext);
_roleRepository = new RoleRepository(_dbContext);
_memberRoleRepository = new MemberRoleRepository(_dbContext);
}
public IQueryable<Role> GetRoles()
{
return _roleRepository.ListAll();
}
public IQueryable<Role> GetRolesForMember(int memberId)
{
var roleIds = _memberRoleRepository.ListAll()
.Where(mr => mr.MemberId == memberId)
.Select(mr => mr.RoleId);
return _roleRepository.ListAll()
.Where(r => roleIds.Contains(r.Id));
}
}
每小时我会得到以下两到三个异常。重新启动项目会立即解决问题,直到下一个问题。
值得注意的是,我在项目的其他任何地方都没有任何数据访问问题。
对于可能导致此问题的任何建议都是最受欢迎的。
答案 0 :(得分:5)
Gert Arnold的评论是正确的。你必须意识到RoleProvider
作为单身人运行。每次重新启动应用程序时,都会导致构建此类的新实例。只要您的应用程序正在运行,它就会继续使用这个单一实例。
在我意识到这个类作为单例运行之前,我曾经遇到过与你问题相同的异常。掌握了这些知识之后,让这些例外消失并不困难:
public class Educ8RoleProvider : RoleProvider
{
//private readonly Educ8DbContext _dbContext = new Educ8DbContext();
//private readonly Authorization _authorization;
public Educ8RoleProvider()
{
//_authorization = new Authorization(_dbContext);
}
private Authorization GetAuthorization()
{
return new Authorization(new Educ8DbContext());
}
public override void Initialize(string name, NameValueCollection config)
{
try
{
base.Initialize(name, config);
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
throw;
}
}
public override bool IsUserInRole(string username, string roleName)
{
return GetRolesForUser(username)
.Any(r => r.ToLower() == roleName);
}
public override string[] GetRolesForUser(string username)
{
return GetAuthorization().GetRolesForMember(username)
.Select(r => r.Name).ToArray();
}
}
基本上,您希望确保RoleProvider
上的每个方法调用都适用于全新的DbContext
实例。这样,RoleProvider
单例不会挂在一个陈旧的DbContext
上。