事务和 ASP.NET 会员 API

发布于 2024-11-03 18:39:05 字数 435 浏览 2 评论 0原文

希望你们中有人去过那里。我需要做一些涉及多个表的数据库工作。我正在使用 SubSonic 3 SimpleRepository 来更新/访问记录。现在,在更新表的调用之间,我调用 System.Web.Security.Roles 方法来进行一些查找。我使用单个存储库对象来执行 DAO 上的所有更新,但是当我的代码命中 User.IsInRole("blahblah") 时,它会抛出异常 MSDTC 在服务器上不可用

我知道发生这种情况是因为 SimpleRepository 正在使用不同的连接,而 Membership API 对象正在使用另一个连接。

有没有办法解决这个问题,或者我是否必须将 Membership API 对象包装在我自己的类中?

Hope someone of you have been there. I need to do some database stuff which involve multiple tables. I am using SubSonic 3 SimpleRepository to update/access records. Now in between calls to update tables I am calling System.Web.Security.Roles methods to do some lookup. I am using single repository object to do all the updates on DAOs, but when my code hits User.IsInRole("blahblah") it throws exception MSDTC is not available on the server.

I understand it is happening because SimpleRepository is using different connection and Membership API objects are using another connection.

Is there a way to get around it or do I have to wrap the Membership API objects in my own classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-11-10 18:39:05
  • 您可以配置成员资格/角色提供程序以使用您需要的连接字符串。

  • 您可以从任何此提供程序继承,并使用事务范围包装每个方法的调用,从外部控制。

  • You can configure membership/roles provider(s) to use the connection string you need.

  • You can inherit from any of this provider(s) and wrap the call of each method with transaction scope, controlled from outside.

隔岸观火 2024-11-10 18:39:05

HttpContext.Current.User.IsInRole() 为通过 FormsAuthentication 进行身份验证的用户调用 RolePrincipal.IsInRole()。在内部,RolePrincipal.IsInRole() 调用 SqlRoleProvider.GetRolesForUser(),这会在方法内创建和销毁 SqlConnection 对象。

SQL Server 上可能有其他解决方案来解决此问题,但从 .NET 方面来看,我只看到以下选项:

  • 实现您自己的角色提供程序,以便您可以自己管理与数据库的连接。
  • 实现您自己的 IPrincipal 对象,以便您可以自己管理与数据库的连接。
  • 在开始 SubSonic 交易之前预取角色,并根据需要检查相关角色的列表。
  • 您甚至可能不需要存储角色,因为 RolePrincipal.IsInRole() 会在调用时缓存角色,并且仅在缓存为空或无效时才会访问数据库。在开始事务之前调用 IsInRole() 将预先填充 RolePrincipal 对象的缓存,这意味着 SubSonic 事务中间的后续调用将从缓存中提取角色连接到数据库来获取它们。

我真的不相信最后一个想法是好的,因为我确信有很多方法可能会出错。我认为最简单的解决方案是在开始 SubSonic 事务之前预取角色。

我希望这有帮助。

编辑:为了完整起见,下面是 Reflector 中看到的 RolePrincipal.IsInRole() 的实现:

public bool IsInRole(string role)
{
    if (this._Identity == null)
    {
        throw new ProviderException(SR.GetString("Role_Principal_not_fully_constructed"));
    }
    if (!this._Identity.IsAuthenticated || (role == null))
    {
        return false;
    }
    role = role.Trim();
    if (!this.IsRoleListCached)
    {
        this._Roles.Clear();
        foreach (string str in Roles.Providers[this._ProviderName].GetRolesForUser(this.Identity.Name))
        {
            if (this._Roles[str] == null)
            {
                this._Roles.Add(str, string.Empty);
            }
        }
        this._IsRoleListCached = true;
        this._CachedListChanged = true;
    }
    return (this._Roles[role] != null);
}

HttpContext.Current.User.IsInRole() calls RolePrincipal.IsInRole() for a user authenticated via FormsAuthentication. Internally RolePrincipal.IsInRole() calls SqlRoleProvider.GetRolesForUser(), which creates and destroys the SqlConnection object within the method.

There may be other solutions on SQL Server to get around this problem, but from the .NET side of the fence I only see the following options:

  • Implement your own role provider so that you can manage connections to the database yourself.
  • Implement your own IPrincipal object so that you can manage connections to the database yourself.
  • Pre-fetch the roles before starting your SubSonic transaction and check the list for the relevant role as needed.
  • You may not even need to store the roles, since RolePrincipal.IsInRole() caches the roles when it is called and only goes out to the database if the cache is empty or invalidated. Calling IsInRole() prior to starting your transaction will pre-populate the RolePrincipal object's cache which means subsequent calls in the middle of the SubSonic transaction will pull the roles from the cache instead of connecting to the database to get them.

I'm really not convinced that the last idea is a good one, as I'm sure there are plenty of ways this can go wrong. I think the easiest solution is to pre-fetch the roles prior to beginning the SubSonic transaction.

I hope that helps.

EDIT: For completeness, here is the implementation of RolePrincipal.IsInRole() as seen in Reflector:

public bool IsInRole(string role)
{
    if (this._Identity == null)
    {
        throw new ProviderException(SR.GetString("Role_Principal_not_fully_constructed"));
    }
    if (!this._Identity.IsAuthenticated || (role == null))
    {
        return false;
    }
    role = role.Trim();
    if (!this.IsRoleListCached)
    {
        this._Roles.Clear();
        foreach (string str in Roles.Providers[this._ProviderName].GetRolesForUser(this.Identity.Name))
        {
            if (this._Roles[str] == null)
            {
                this._Roles.Add(str, string.Empty);
            }
        }
        this._IsRoleListCached = true;
        this._CachedListChanged = true;
    }
    return (this._Roles[role] != null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文