深入分析WCF事务投票实现方式

开发 开发工具
WCF事务投票的实现方法是一个比较简单但是在实际应用中又非常重要的一个操作技术。在这里我们将会和大家一起解读这一技术的具体应用。

我们知道事务是通过参与方进行WCF事务投票(Voting)来决定 "提交(Complete)" 或者 "回滚(Rollback)" 操作的。默认情况下,WCF 通过 OperationBehavior(TransactionAutoComplete = true) 来完成投票动作。(TransactionAutoComplete = true 是缺省值,不需要显式声明。)

我们将服务方法默认的 TransactionAutoComplete=true 改为 false,看看结果 。

// ---- Service1 -----  
[ServiceContract(SessionModeSessionMode=SessionMode.Required)]  
public interface IService1  
{  
[OperationContract]  
[TransactionFlow(TransactionFlowOption.Allowed)]  
void Test();  
}  
public class MyService1 : IService1  
{  
[OperationBehavior(TransactionScopeRequired=true, 
TransactionAutoComplete=false)]   public void Test()   {   string connStr = "server=(local);uid=sa;pwd=sa;database=temp";   using (SqlConnection conn = new SqlConnection(connStr))   {   conn.Open();   SqlCommand cmd = new SqlCommand("insert into [User] 
([name]) values (@name)",   
conn);   cmd.Parameters.Add(new SqlParameter("@name", "ZhangSan"));   cmd.ExecuteNonQuery();   }   }   }   // ---- Service2 -----   [ServiceContract(SessionMode = SessionMode.Required)]   public interface IService2   {   [OperationContract]   [TransactionFlow(TransactionFlowOption.Allowed)]   void Test();   }   public class MyService2 : IService2   {   [OperationBehavior(TransactionScopeRequired = true, 
TransactionAutoComplete = false)]  
public void Test()   {   string connStr = "server=(local);uid=sa;pwd=sa;database=temp";   using (SqlConnection conn = new SqlConnection(connStr))   {   conn.Open();   SqlCommand cmd = new SqlCommand("insert into Account ([user], 
[money]) values (@user, @money)",   
conn);   cmd.Parameters.Add(new SqlParameter("@user", "ZhangSan"));   cmd.Parameters.Add(new SqlParameter("@money", 100));   cmd.ExecuteNonQuery();   }   }   }   public class WcfTest   {   public static void Test()   {   // ---- Host -----   AppDomain.CreateDomain("Server").DoCallBack(delegate   {   NetTcpBinding bindingServer = new NetTcpBinding();   bindingServer.TransactionFlow = true;   ServiceHost host1 = new ServiceHost(typeof(MyService1), 
new Uri("net.tcp://localhost:8080"));  
host1.AddServiceEndpoint(typeof(IService1), bindingServer, "");   host1.Open();   ServiceHost host2 = new ServiceHost(typeof(MyService2), 
new Uri("net.tcp://localhost:8081"));  
host2.AddServiceEndpoint(typeof(IService2), bindingServer, "");   host2.Open();   });   // ---- Client -----   NetTcpBinding bindingClient = new NetTcpBinding();   bindingClient.TransactionFlow = true;   IService1 client1 = ChannelFactory<IService1>.CreateChannel(bindingClient,    new EndpointAddress("net.tcp://localhost:8080"));   IService2 client2 = ChannelFactory<IService2>.CreateChannel(bindingClient,    new EndpointAddress("net.tcp://localhost:8081"));   using (TransactionScope scope = new TransactionScope())   {   try   {   client1.Test();   client2.Test();   scope.Complete();   }   finally   {   (client1 as IDisposable).Dispose();   (client2 as IDisposable).Dispose();   }   }   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.

运行结果表明事务无法提交,触发 TransactionAbortedException 异常,显示 "事务终止"。那么除了默认被称之为 "声明投票(Declarative voting)" 的方式外,我们还能怎么做?OperationContext 有个 SetTransactionComplete() 方法,允许我们在代码中完成WCF事务投票行为。这种投票方式更加灵活,便于我们在代码中做出更多的控制,被称之为 "显式投票(Explicit voting)"。

在上面两个 Test() 方法的***一行,添加 "OperationContext.Current.SetTransactionComplete();",再次运行,事务被正确提交。

[OperationBehavior(TransactionScopeRequired=true, 
TransactionAutoComplete=false)]  
public void Test()   {   // ...   OperationContext.Current.SetTransactionComplete();   }    ... 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

接下来,我们设想另外一种情况。事务不由 Client 发起,在 Service1.Test() 调用 Service2.Test(),那么事务会是个什么样子呢?Service1、Service2 的参数 "OperationBehavior(TransactionScopeRequired = true)" 决定了如果没有外界传入的环境事务,那么会自动创建一个根事务。所以 Service1.Test() 会创建一个根事务,而 Service2.Test() 会参与这个事务。可问题在于 Service.Test() 中并没有显示调用 Transaction.Complete,事务能被提交吗?

// ---- Service1 -----  
[ServiceContract]  
public interface IService1  
{  
[OperationContract]  
[TransactionFlow(TransactionFlowOption.Allowed)]  
void Test();  
}  
public class MyService1 : IService1  
{  
[OperationBehavior(TransactionScopeRequired=true)]  
public void Test()  
{  
string connStr = "server=(local);uid=sa;pwd=sa;database=temp";  
using (SqlConnection conn = new SqlConnection(connStr))  
{  
conn.Open();  
SqlCommand cmd = new SqlCommand("insert into [User] 
([name]) values (@name)",   
conn);   cmd.Parameters.Add(new SqlParameter("@name", "ZhangSan"));   cmd.ExecuteNonQuery();   }   InvokeService2();   }   public void InvokeService2()   {   NetTcpBinding bindingClient = new NetTcpBinding();   bindingClient.TransactionFlow = true;   IService2 client2 = ChannelFactory<IService2>.CreateChannel
(bindingClient,   
new EndpointAddress("net.tcp://localhost:8081"));   using (client2 as IDisposable)   {   client2.Test();   }   }   }   // ---- Service2 -----   [ServiceContract]   public interface IService2   {   [OperationContract]   [TransactionFlow(TransactionFlowOption.Allowed)]   void Test();   }   public class MyService2 : IService2   {   [OperationBehavior(TransactionScopeRequired = true)]   public void Test()   {   string connStr = "server=(local);uid=sa;pwd=sa;database=temp";   using (SqlConnection conn = new SqlConnection(connStr))   {   conn.Open();   SqlCommand cmd = new SqlCommand("insert into Account 
([user], [money]) values (@user, @money)",   
conn);   cmd.Parameters.Add(new SqlParameter("@user", "ZhangSan"));   cmd.Parameters.Add(new SqlParameter("@money", 100));   cmd.ExecuteNonQuery();   }   }   }   public class WcfTest   {   public static void Test()   {   // ---- Host -----   AppDomain.CreateDomain("Server").DoCallBack(delegate   {   NetTcpBinding bindingServer = new NetTcpBinding();   bindingServer.TransactionFlow = true;   ServiceHost host1 = new ServiceHost(typeof(MyService1), 
new Uri("net.tcp://localhost:8080"));  
host1.AddServiceEndpoint(typeof(IService1), bindingServer, "");   host1.Open();   ServiceHost host2 = new ServiceHost(typeof(MyService2), 
new Uri("net.tcp://localhost:8081"));  
host2.AddServiceEndpoint(typeof(IService2), bindingServer, "");   host2.Open();   });   // ---- Client -----   NetTcpBinding bindingClient = new NetTcpBinding();   bindingClient.TransactionFlow = true;   IService1 client1 = ChannelFactory<IService1>.CreateChannel
(bindingClient,   
new EndpointAddress("net.tcp://localhost:8080"));   try   {   client1.Test();   }   finally   {   (client1 as IDisposable).Dispose();   }   }  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.

运行结果表明,事务被正确提交。看来这和客户端使用 TransactionScope 必须显式调用 Complete() 有所不同。同样,如果将 Service2.Test() 设为 TransactionAutoComplete=false,在不调用 "OperationContext.Current.SetTransactionComplete();" 的情况下,也会触发事务失败异常。

以上就是我们为大家介绍的WCF事务投票的相关实现方法。

【编辑推荐】

  1. WCF MSMQ队列基本概念简述
  2. PDA访问WCF实现重点在过程
  3. WCF标准终结点基本概念剖析
  4. WCF回调操作是鸡应用技巧讲解
  5. WCF元数据交换应用技巧分享
责任编辑:曹凯 来源: 豆豆网
相关推荐

2021-03-17 00:05:50

分布式事务提交

2022-04-12 08:30:45

TomcatWeb 应用Servlet

2010-09-07 14:21:22

PPPoE协议

2011-03-23 11:01:55

LAMP 架构

2010-03-05 13:38:13

Python数据转换

2010-01-08 16:58:49

网管交换机

2010-03-08 14:53:48

Linux分区

2011-09-01 13:51:52

JavaScript

2023-02-01 08:13:30

Redis内存碎片

2022-08-30 07:00:18

执行引擎Hotspot虚拟机

2009-06-10 18:12:38

Equinox动态化OSGi动态化

2009-12-16 16:39:01

Visual Stud

2009-12-14 14:50:46

Ruby传参数

2021-10-29 16:36:53

AMSAndroidActivityMan

2018-12-18 10:11:37

软件复杂度软件系统软件开发

2011-09-13 09:08:22

架构

2015-08-03 09:54:26

Java线程Java

2018-10-25 15:24:10

ThreadLocal内存泄漏Java

2021-04-13 12:55:06

SpringMVC解析器接口

2020-12-07 06:23:48

Java内存
点赞
收藏

51CTO技术栈公众号