WCF分布式开发必备知识系列文章3:Enterpise Services(3)
using System;
2using System.Collections.Generic;
3using System.Text;
4using EnterpriseServicesCOM;
5namespace EnterpriseServicesClient
6{
7 //客户端为控制台程序,方便测试
8 class EnterpriseServicesClient
9 {
10 static void Main(string[] args)
11 {
12 //创建COM对象,测试组件服务的一般调用
13 using (EnterpriseServicesCOM.EnterpriseServicesCOMPlus enterpriseServicesCOM = new EnterpriseServicesCOMPlus())
14 {
15 for (int i = 0; i < 10; i++)
16 {
17
18 string sName = "Frank Xu Lei";
19 string sMessage = enterpriseServicesCOM.SayHello(sName);//调用
20 Console.WriteLine(sMessage);
21 }
22 }
23 //创建COM对象,测试组件服务的事物特性
24 using (EnterpriseServicesCOM.EnterpriseServicesCOMPlusTransaction enterpriseServicesCOMTransaction = new EnterpriseServicesCOMPlusTransaction())
25 {
26 //第一次事务操作成功,字段长度合法
27 if (enterpriseServicesCOMTransaction.AddUser("FrankXu", "ShangHai"))
28 {
29 Console.WriteLine("Transaction1 is successful");
30 }
31 else
32 {
33 Console.WriteLine(Console.WriteLine("Transaction1 is failed"));
34 }
35 //第2次事务操作失败
36 if (enterpriseServicesCOMTransaction.AddUser("FrankXuLei2009", "ShangHai2009"))
37 {
38 Console.WriteLine("Transaction1 is successful");
39 }
40 else
41 {
42 Console.WriteLine(Console.WriteLine("Transaction1 is failed"));
43 }
44
45 }
46 Console.ReadLine();
47 }
48 }
49}
测试事物特性的代码分了两种情况,第一次调用新增用户的方法事务成功,字符长度合法.第2次事务则提交失败,字符插入数据库异常.事务回滚.
四.总结
运行结果如下图:
Transaction1执行成功,Transaction2执行失败.ContextUtil.SetAbort();设置上下文环境的状态信息,事物终止,该语句使所有的数据库操作回滚,数据库未增加新记录.
今天我们详细学习了.NET Enterprise Services基本知识,以及它和COM+的关系,熟悉了.NET Enterprise Services的事务特性,详细的了解COM+组件服务的技术细节.希望能给大家一些启发和帮助.欢迎交流.下一节打算写分布式编程的另外一个技术Web Service.会附加一部分WSE的部分,另外延续本文的习惯,我会上穿完整的代码/Files/frank_xl/EnterpriseServices.rar.本文参考了大牛Tim Ewald的文章COM+ Integration: How .NET Enterprise Services Can Help You Build Distributed Applications.很不错的文章,好象还没人翻译,打算周末抽时间翻译一下一起放出来,与大家分享.有兴趣的朋友可以关注.~翻译完毕:http://www.cnblogs.com/frank_xl/archive/2009/02/15/1391046.html
WCF分布式开发必备知识系列文章快速学习列表:
WCF分布式开发必备知识系列文章1:MSMQ消息队列
http://www.xueit.com/html/2009-02/21_625_00.html
WCF分布式开发必备知识系列文章2:Net Remoting
http://www.xueit.com/html/2009-02/21_626_00.html
WCF分布式开发必备知识系列文章3:Enterpise Services
http://www.xueit.com/html/2009-02/21_627_00.html
WCF分布式开发必备知识系列文章4:Web Service
http://www.xueit.com/html/2009-02/21_628_00.html