事务和 ASP.NET 会员 API
希望你们中有人去过那里。我需要做一些涉及多个表的数据库工作。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以配置成员资格/角色提供程序以使用您需要的连接字符串。
您可以从任何此提供程序继承,并使用事务范围包装每个方法的调用,从外部控制。
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.
HttpContext.Current.User.IsInRole()
为通过 FormsAuthentication 进行身份验证的用户调用RolePrincipal.IsInRole()
。在内部,RolePrincipal.IsInRole()
调用SqlRoleProvider.GetRolesForUser()
,这会在方法内创建和销毁SqlConnection
对象。SQL Server 上可能有其他解决方案来解决此问题,但从 .NET 方面来看,我只看到以下选项:
IPrincipal
对象,以便您可以自己管理与数据库的连接。RolePrincipal.IsInRole()
会在调用时缓存角色,并且仅在缓存为空或无效时才会访问数据库。在开始事务之前调用IsInRole()
将预先填充RolePrincipal
对象的缓存,这意味着 SubSonic 事务中间的后续调用将从缓存中提取角色连接到数据库来获取它们。我真的不相信最后一个想法是好的,因为我确信有很多方法可能会出错。我认为最简单的解决方案是在开始 SubSonic 事务之前预取角色。
我希望这有帮助。
编辑:为了完整起见,下面是 Reflector 中看到的
RolePrincipal.IsInRole()
的实现:HttpContext.Current.User.IsInRole()
callsRolePrincipal.IsInRole()
for a user authenticated via FormsAuthentication. InternallyRolePrincipal.IsInRole()
callsSqlRoleProvider.GetRolesForUser()
, which creates and destroys theSqlConnection
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:
IPrincipal
object so that you can manage connections to the database yourself.RolePrincipal.IsInRole()
caches the roles when it is called and only goes out to the database if the cache is empty or invalidated. CallingIsInRole()
prior to starting your transaction will pre-populate theRolePrincipal
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: