我的角色提供者看起来像下面的类(删节)。我在 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));
}
}
每小时我会遇到大约两到三个以下异常。重新启动项目立即解决问题,直到下一个。
- 不允许更改“ConnectionString”属性。连接的当前状态为关闭。
- 创建模型时不能使用上下文。
- 基础提供程序在打开时失败。
值得注意的是,在项目的其他任何地方,我的数据访问问题绝对为零。
任何关于可能导致这种情况的建议都将受到欢迎。