有什么能首先使用EF code在自定义RoleProvider导致许多数据访问异常? [英] What could cause many data access exceptions using EF code first in a custom RoleProvider?

查看:95
本文介绍了有什么能首先使用EF code在自定义RoleProvider导致许多数据访问异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的角色提供看起来像下面(有删节)类。我运行在IIS防爆preSS和SQL 2012前preSS,从2012 VS,我也得到看似随意的例外在这个角色提供code,众说纷纭。

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

我得到以下异常大约2至3每隔一小时。重新启动该项目立即解决问题,直到下一个。

I get about two to three of the following exceptions every hour. Restarting the project immediately solves the problem, until the next one.


  • 不允许更改'的ConnectionString属性。该
    连接的当前状态是关闭的。

  • 上下文不能被创建模型,同时使用。

  • 基础提供开放式失败。

这可能是值得指出的是,我有绝对的零数据访问项目其他任何地方的问题。

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.

推荐答案

格特·阿诺德是在正确的轨道,他的评论。你必须认识到, RoleProvider 运行作为一个单身。每次重新启动您的应用程序时,它导致将建造这个类的一个新实例。只要你的应用程序运行时,它不断用这个单一实例。

Gert Arnold is on the right track with his comment. You have to realize that the RoleProvider runs as a singleton. Each time you restart your app, it is causing a new instance of this class to be constructed. As long as your application is running, it keeps using this one single instance.

我曾经得到同样类型的异常是你的问题之前,我意识到,这个类运行作为一个单身。之后,你有知识,这是不难作出这些异常消失:

I used to get the same kinds of exceptions as in your question before I realized that this class runs as a singleton. After you have that knowledge, it is not difficult to make these exceptions go away:

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 ,获取陈旧。

Essentially, you want to make sure that each method call on the RoleProvider works with a brand new DbContext instance. This way, the RoleProvider singleton is not hanging onto a single DbContext that gets stale.

这篇关于有什么能首先使用EF code在自定义RoleProvider导致许多数据访问异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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