在WCF中使用事务处理
来源:互联网 发布:数据库两个主键 编辑:程序博客网 时间:2024/05/15 14:53
WCF在预定义绑定中实现了标准的WSAtomicTranscation(WS-AT)协议和Microsoft专有的OleTx协议,这些协议可以用来在消息中加入事务状态的信息。我们可以指定将一个操作的代码放在事务范围里执行。
我们需要在Binding的配置节里指定,transcationFlow=true:
<bindings> <wsHttpBinding> <binding name="TranscationBindingConfig" transactionFlow="true"> <security mode="None" /> </binding> </wsHttpBinding></bindings>
注意:任何被指定的必须在一个事务范围里执行的操作(OperationContract)都不能标记为单向方法(IsOneWay=true),因为在操作结束时,有关事务状态的信息必须传回给调用者。开发者也可以指出,当没有异常发生时,WCF应该自动为操作提交事务。
namespace WcfTranServiceLib{ [ServiceContract] public interface IDAOService { [OperationContract(IsOneWay=false)] [TransactionFlow(TransactionFlowOption.Mandatory)] void InsAndUpdProduct(Products product); [OperationContract] [TransactionFlow(TransactionFlowOption.Mandatory)] void InsAndUpdSupplier(Suppliers supplier); [OperationContract] List<Products> GetProducts(string productID); [OperationContract] List<Suppliers> GetSuppliers(string supplierID); }}
如果需要在服务实现的操作需要自己来提交事务,可以使用WCF的静态System.ServiceModel.OperationContext对象:
public class MyServiceType{ [OperationBehavior(TranscationScopeRequire=true, TrancationAutoComplete=false)] void MyMethod() { // 其他的业务逻辑 OperationContext.Current.SetTranscationComplete(); }}
而客户端开发者可以使用System.Transcation命名空间提供的语法将服务的操作放进一个事务范围内。
using(TranscationScope ts = new TranscationScope(TranscationScopeOption.RequireNew)){ client.DoSomething(); ... ts.Complete();}client.Close();
如果服务的操作支持事务,而且也配置了支持传送有关事务状态的绑定,那么客户端的事务直到服务操作决定提交时才会提交(TranscationScope的Complete被调用)。相反地,服务在客户端事务范围里对事务资源所做的任何操作都将被回滚。(客户端处理的过程中出现任何异常)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Transactions;namespace WcfTranClient{ class Program { static void Main(string[] args) { try { var client = new DaoSvc.DAOServiceClient(); using (var ts = new TransactionScope()) { DaoSvc.Suppliers s = new DaoSvc.Suppliers(); s.SupplierID = "S02"; s.CompanyName = "CompanyName1"; s.Address = "Address"; s.Phone = "123456"; client.InsAndUpdSupplier(s); DaoSvc.Products p = new DaoSvc.Products(); p.ProductID = "P02"; p.ProductName = "ProductName1"; p.UnitPrice = 5; p.SupplierID = "S02"; client.InsAndUpdProduct(p); //throw new Exception("Test Exception"); ts.Complete(); } var ps = client.GetProducts(null); Console.WriteLine("Count of products: {0}", ps.Count); var ss = client.GetSuppliers(null); Console.WriteLine("Count of suppliers: {0}", ss.Count); client.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } } }}
- 在WCF中使用事务处理
- 在jsp中使用事务处理
- Oraclee 事务处理以及在c#中使用oracle的事务处理
- 在三层架构中使用事务处理
- 在MSMQ中使用事务处理消息
- 在MSMQ中使用事务处理消息
- 【ITOO 4】WCF中,分布式事务处理
- 在WCF中使用消息队列
- 关于在sl中使用WCF注意事项
- 在WCF Uri中使用特殊字符
- ASP中使用事务处理
- ASP中使用事务处理
- ASP中使用事务处理
- ASP中使用事务处理
- 。Net中使用事务处理
- MYSQL中使用事务处理
- 在ASP中使用事务处理数据库增/删/改操作
- 在java代码中使用Oracle数据库的事务处理机制
- 由纯CSS打造的23个著名Logo,苹果
- Internet地址概述
- IE6、IE7、IE8之间的js
- 利用NO_MERGE解决数据字典视图访问低效
- bochs调试器安装
- 在WCF中使用事务处理
- 研究生数学建模比赛做的一些matlab脚本(2012基因那题)
- C/C++ 二维指针 矩阵 初始化
- I2C
- xmlhttp.open("GET","file:///C:/Users/Administrator/Desktop/java/sample.xml",false);
- 笔记本机身的材质
- Dijkstra算法|单源最短路径|贪心算法
- android http通信方式连接网络
- 【Homework】【整理】SSS 2012-09-19