My role provider looks like the class below (abridged). I am running on IIS Express, and SQL 2012 Express, from VS 2012, and I get a multitude of seemingly random exceptions in this role provider code.
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();
}
}
Here is a sample of my Authorization
class:
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));
}
}
I get about two to three of the following exceptions every hour. Restarting the project immediately solves the problem, until the next one.
- Not allowed to change the 'ConnectionString' property. The
connection's current state is closed.
- The context cannot be used while the model is being created.
- The underlying provider failed on Open.
It may be worth noting that I have absolutely zero data access issues anywhere else in the project.
Any suggestions on what may be causing this would be most welcome.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…