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

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

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

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

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

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

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

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

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

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

运行结果表明,事务被正确提交。看来这和客户端使用 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

分布式事务提交

2011-03-23 11:01:55

LAMP 架构

2022-04-12 08:30:45

TomcatWeb 应用Servlet

2010-09-07 14:21:22

PPPoE协议

2010-03-05 13:38:13

Python数据转换

2010-01-08 16:58:49

网管交换机

2009-12-14 14:50:46

Ruby传参数

2009-06-10 18:12:38

Equinox动态化OSGi动态化

2009-12-16 16:39:01

Visual Stud

2021-10-29 16:36:53

AMSAndroidActivityMan

2022-08-30 07:00:18

执行引擎Hotspot虚拟机

2010-03-08 14:53:48

Linux分区

2023-02-01 08:13:30

Redis内存碎片

2011-09-01 13:51:52

JavaScript

2009-07-03 11:14:57

2011-06-28 14:11:33

JavaScript

2014-10-30 15:08:21

快速排序编程算法

2009-04-13 16:37:33

JSPWeb标签

2023-08-07 07:44:44

2021-04-13 12:55:06

SpringMVC解析器接口
点赞
收藏

51CTO技术栈公众号