WCF之分布式事务
首先講解一下事務的幾個很重要的性質。
?
CodeWCF?對分布事務提供了良好的支持,這使得我們可以協調多個服務之間的數據完整性。通過?TransactionFlowAttribute、ServiceBehaviorAttribute?和?OperationBehaviorAttribute?這三個特性,我們可以很好地控制事務的相關細節。
TransactionFlowAttribute?的構造參數?"TransactionFlowOption?transactions"?允許我們在強制事務(Mandatory)、允許參與事務(Allowed)和禁止事務(NotAllowed)間進行選擇。
ServiceBehaviorAttribute?提供了多個屬性參與事務控制。TransactionAutoCompleteOnSessionClose?指示當會話(Session)結束時是否自動提交事務(Complete);?ReleaseServiceInstanceOnTransactionComplete?指示事務提交后是否釋放服務實例對象;?TransactionIsolationLevel?用于設置事務隔離方式(isolation?level);?TransactionTimeout?用于設置事務超時時間。
OperationBehaviorAttribute?的?TransactionScopeRequired?屬性是?WCF?分布事務所必需使用的,它表明服務方法必須在事務范圍(transaction?scope)內執行。如果不添加該標記,則意味著服務方法不參與到事務中。TransactionAutoComplete?指示方法正常結束后自動提交事務。
?
?
采用WShttp形式。采用添加服務引用的方式。
?
1 在服務器端配置文件中,要加下如下代碼。 放在? <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 在實現接口類上面加上如下屬性
?
Code[OperationBehavior(TransactionScopeRequired=true)]
?
5 接下來就是客戶端的調用了。
?
Code??using?(TransactionScope?scope?=?new?TransactionScope())
????????????{
?????????????ServiceClient?over?=?new?ServiceClient();
?????????????Data.Computer?p?=over.GetComputer();
????????????
?????????????Console.WriteLine(p.ComputerName);
??????????????Console.Read();
????????????????scope.Complete();??
???????????????
????????????}
?
?
至此。 一個WCF的事務就完成了。
轉載于:https://www.cnblogs.com/z2002m/archive/2008/08/04/1260218.html
總結
- 上一篇: PAMIE:点击网页中的弹出窗口的按钮
- 下一篇: 程序员须知:必须建立个人知识库,它的重要