加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C#中使用TransactionScope?

发布时间:2020-12-16 01:20:41 所属栏目:百科 来源:网络整理
导读:我正在尝试使用TransactionScope,但继续获得下面的例外. 如果这很重要,应用程序将在与数据库不同的计算机上运行.我正在使用SQL Server 2005. Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network
我正在尝试使用TransactionScope,但继续获得下面的例外.
如果这很重要,应用程序将在与数据库不同的计算机上运行.我正在使用SQL Server 2005.

Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration
for MSDTC using the Component Services Administrative tool.

using (TransactionScope tsTransScope = new TransactionScope())
{
    //Do stuff here
    tsTransScope.Complete();
}

编辑

我根据反馈做了一些改变.现在我收到这个错误:

“Error HRESULT E_FAIL has been returned from a call to a COM component.”
“Communication with the underlying transaction manager has failed.”


我认为接受的答案解决了我遇到的最初问题.第二个错误似乎特定于Entity Framework.我会发布另一个问题.

以下是客户端上的属性:
Client http://www.portnine.com/data/images/Misc/client.jpg

以下是服务器上的属性:
Server http://www.portnine.com/data/images/Misc/server.jpg

解决方法

您需要按照此 Microsoft TechNet Article中的说明启用网络DTC访问.可能必须在数据库和应用程序服务器上进行此更改.通常DTC已经打开数据库服务器,所以我先看看应用程序服务器.

以下是我们使用的屏幕截图,但“允许远程管理”选项除外:

我没有遇到你现在遇到的HRESULT E_Fail问题,但是这篇文章于XP SP2 and transactions提出了这个有趣的建议:

Another configuration setting that you
need to be aware (although I consider
it to be an uncommon scenario) is
RestrictRemoteClients registry key. If
the value of this key is set to 2
(RPC_RESTRICT_REMOTE_CLIENT_HIGH) then
MSDTC network transactions will not be
able to work properly. MSDTC supports
only RPC_RESTRICT_REMOTE_CLIENT_NONE
(0) and
RPC_RESTRICT_REMOTE_CLIENT_DEFAULT (1)
values. See
07002
for more info on
RestrictRemoteClients.

最后,虽然不是特定于您的问题,但是关于使用TransactionScope类的一个非常重要的事情是它的默认设置是使用Transaction Isolation Level of Serializable. Serializable是最严格的隔离级别,坦率地说它被选为默认值令人惊讶.如果您不需要这种级别的锁定,我强烈建议在实例化TransactionScope时将隔离级别设置为限制较少的选项(ReadCommitted):

var scopeOptions = new TransactionOptions();
scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
scopeOptions.Timeout = TimeSpan.MaxValue;

using (var scope = new TransactionScope(TransactionScopeOption.Required,scopeOptions))
{
    // your code here
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读