如果在C#中使用TransactionScope類(lèi)(分布式事務(wù)),則須注意如下事項(xiàng):
1、在項(xiàng)目中引用using System.Transactions命名空間(先要在添加net組件的引用);
2、具體示例如下:

/**//// <summary>
/// 發(fā)送消息
/// </summary>
/// <param name="sendUserId"></param>
/// <param name="toUser">格式7FFA3AF2-E74B-4174-8403-5010C53E49A7|userName,7FFA3AF2-E74B-4174-8403-5010C53E49A7|userName</param>
/// <param name="content"></param>
/// <param name="sendedStatus">表示已送</param>
/// <returns></returns>
public static int sendMessage(string sendUserId, string toUser, string content, string sendedStatus)

{
int receiveCount = 0;
TransactionOptions transactionOption = new TransactionOptions();

//設(shè)置事務(wù)隔離級(jí)別
transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

// 設(shè)置事務(wù)超時(shí)時(shí)間為60秒
transactionOption.Timeout = new TimeSpan(0, 0, 60);

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))

{
try

{
//在這里實(shí)現(xiàn)事務(wù)性工作
//發(fā)送消息
insertMessage(sendUserId, toUser, content, sendedStatus);

//在接收信息表中插入記錄
receiveCount += insertReceiveMessage(userids[0], sendUserId, content, "0");
// 沒(méi)有錯(cuò)誤,提交事務(wù)
scope.Complete();
}

catch (Exception ex)
{
throw new Exception("發(fā)送信息異常,原因:"+ex.Message);
}finally{
//釋放資源
scope.Dispose();
}
}
return receiveCount;
}


3、對(duì)MSDTC組件設(shè)置:
步驟:
在控制面板--->管理工具--->服務(wù) 中,開(kāi)啟Distributed Transaction Coordinator 服務(wù)。
a.控制面板->管理工具->組件服務(wù)->計(jì)算機(jī)->我的電腦->右鍵->屬性
b.選擇MSDTC頁(yè), 確認(rèn)"使用本地協(xié)調(diào)器"
c.點(diǎn)擊下方"安全配置"按鈕
d.勾選: "允許網(wǎng)絡(luò)DTC訪問(wèn)","允許遠(yuǎn)程客戶(hù)端","允許入站","允許出站","不要求進(jìn)行身份驗(yàn)證".
e.對(duì)于數(shù)據(jù)庫(kù)服務(wù)器端, 可選擇"要求對(duì)呼叫方驗(yàn)證"
f.勾選:"啟用事務(wù)Internet協(xié)議(TIP)事務(wù)"。
g.在雙方防火墻中增加MSDTC.exe例外
可用命令行: netsh firewall set allowedprogram %windir%\system32\msdtc.exe MSDTC enable
4、重啟IIS服務(wù)器。
posted on 2009-03-17 17:02
aisoft 閱讀(17039)
評(píng)論(3) 編輯 收藏 所屬分類(lèi):
.NET技術(shù)