WCF 学习总结8 –- WCF 事务处理

本文介绍了如何在WCF服务中配置并使用事务处理功能,包括WSAtomicTransaction和OleTx协议的应用,以及如何通过代码控制事务的提交和回滚。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 学习总结1 -- 简单实例

WCF 学习总结2 -- 配置WCF

WCF 学习总结3 -- 实例模式

WCF 学习总结4 -- 用Duplex实现消息广播

WCF 学习总结5 -- 消息拦截实现用户名验证

WCF 学习总结6 -- WCF参数与返回值

WCF 学习总结7 -- 流模式实现文件上传

WCF 学习总结8 –- WCF 事务处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值