WCF之分布式事务
首先講解一下事務(wù)的幾個(gè)很重要的性質(zhì)。
?
CodeWCF?對(duì)分布事務(wù)提供了良好的支持,這使得我們可以協(xié)調(diào)多個(gè)服務(wù)之間的數(shù)據(jù)完整性。通過?TransactionFlowAttribute、ServiceBehaviorAttribute?和?OperationBehaviorAttribute?這三個(gè)特性,我們可以很好地控制事務(wù)的相關(guān)細(xì)節(jié)。
TransactionFlowAttribute?的構(gòu)造參數(shù)?"TransactionFlowOption?transactions"?允許我們?cè)趶?qiáng)制事務(wù)(Mandatory)、允許參與事務(wù)(Allowed)和禁止事務(wù)(NotAllowed)間進(jìn)行選擇。
ServiceBehaviorAttribute?提供了多個(gè)屬性參與事務(wù)控制。TransactionAutoCompleteOnSessionClose?指示當(dāng)會(huì)話(Session)結(jié)束時(shí)是否自動(dòng)提交事務(wù)(Complete);?ReleaseServiceInstanceOnTransactionComplete?指示事務(wù)提交后是否釋放服務(wù)實(shí)例對(duì)象;?TransactionIsolationLevel?用于設(shè)置事務(wù)隔離方式(isolation?level);?TransactionTimeout?用于設(shè)置事務(wù)超時(shí)時(shí)間。
OperationBehaviorAttribute?的?TransactionScopeRequired?屬性是?WCF?分布事務(wù)所必需使用的,它表明服務(wù)方法必須在事務(wù)范圍(transaction?scope)內(nèi)執(zhí)行。如果不添加該標(biāo)記,則意味著服務(wù)方法不參與到事務(wù)中。TransactionAutoComplete?指示方法正常結(jié)束后自動(dòng)提交事務(wù)。
?
?
采用WShttp形式。采用添加服務(wù)引用的方式。
?
1 在服務(wù)器端配置文件中,要加下如下代碼。 放在? <system.serviceModel>里面
Code??<bindings>
??????<wsHttpBinding>
????????<binding?name="MyBinding"?transactionFlow="True"?/>
??????</wsHttpBinding>
????</bindings> 2? 使用上面新增的BINDINGS Code
?<endpoint?address=""?binding="wsHttpBinding"?bindingConfiguration="MyBinding"?contract="Interface.IService"?>
?
3 在接口的方法上面加上如下屬性
Code?[TransactionFlow(TransactionFlowOption.Mandatory)]
?
4 在實(shí)現(xiàn)接口類上面加上如下屬性
?
Code[OperationBehavior(TransactionScopeRequired=true)]
?
5 接下來就是客戶端的調(diào)用了。
?
Code??using?(TransactionScope?scope?=?new?TransactionScope())
????????????{
?????????????ServiceClient?over?=?new?ServiceClient();
?????????????Data.Computer?p?=over.GetComputer();
????????????
?????????????Console.WriteLine(p.ComputerName);
??????????????Console.Read();
????????????????scope.Complete();??
???????????????
????????????}
?
?
至此。 一個(gè)WCF的事務(wù)就完成了。
轉(zhuǎn)載于:https://www.cnblogs.com/z2002m/archive/2008/08/04/1260218.html
總結(jié)
- 上一篇: PAMIE:点击网页中的弹出窗口的按钮
- 下一篇: 程序员须知:必须建立个人知识库,它的重要