WCF事务演示经典实例剖析

开发 开发工具
我们今天就为大家列举了一个比较经典的WCF事务演示,以方便大家对这方面的知识有一个详细的了解,并加深对WCF的认知程度。

WCF开发工具功能特点比较突出,其优势突出的功能为它在开发领域中占据着一个比较重要的地位。在这里我们将会通过对WCF事务演示的解读,来充分的了解一下这一开发平台的应用方式。

下面的这段代码就是WCF事务演示的经典示例:

  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. }  
  24. }  
  25. // -------- Service2 -----------------  
  26. [ServiceContract]  
  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)]  
  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. }  

以上就是我们为大家带来的WCF事务演示。

【编辑推荐】

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

2010-02-22 16:09:33

WCF宿主

2009-11-06 15:02:47

WCF契约查询

2009-11-06 09:30:35

WCF服务框架

2009-11-09 13:12:14

WCF事物操作

2010-02-22 15:27:05

WCF数据契约

2009-11-09 11:15:06

WCF消息队列

2010-02-23 09:34:15

WCF重载

2010-02-22 10:01:11

WCF消息处理

2009-11-09 09:23:10

WCF数据契约

2009-11-06 11:07:52

WCF事务属性

2010-03-02 16:28:11

WCF发布订阅

2009-12-07 18:43:29

WCF框架

2009-12-08 17:56:16

WCF配置

2009-11-05 09:35:54

WCF体系架构

2009-11-05 10:07:37

WCF设计模式

2010-02-22 16:26:47

WCF传输数据

2009-11-06 09:14:14

WCF可靠性

2009-11-05 16:34:37

WCF序列化

2023-09-28 11:42:15

2010-02-23 16:07:39

点赞
收藏

51CTO技术栈公众号