版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Message是WCF信道层提供的一个类,在Message类里,数据被标识成一个XML Information Set, 简称为InfoSet。当数据从客户端传输给服务端时,binding里指定的消息编码协议将决定包含客户端所提供数据的Message对象将以何种形式提供给服务(服务端到客户端也一样)。然而,所有标准的binding都会使用将Message对象表示成XML InfoSet的编码协议。根据预定义binding的编码协议,XML InfoSet可能会使用各种标准的XML文本编码、MTOM或者二进制格式。也就是说从WCF应用层到传输将会是下面这个过程:序列化(XML InfoSet)编
2、码(Text/MTOM/Binary)传输解码(XML InfoSet)反序列化WCF提供了两种XML序列化的工具来完成:DataContractSerializer和XmlSerializer。那么,WCF支持哪些类型的参数和返回值呢?当然WCF里推荐使用DataContract和DataMember来定义复杂的自定义对象,但是在WebService时代里,我们知道只要实体类或者其父类被标识为Serializable就支持序列化了,就可以作为WebMethod的参数或者返回值了。在有些场景,已经生成好众多实体类,又不想一个个为每个属性加上DataMember,自然很怀念Serializabl
3、e。OK,下面来看看代码:1. 服务契约:c-sharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.Linq;4. usingSystem.Runtime.Serialization;5. usingSystem.ServiceModel;6. usingSystem.ServiceModel.Web;7. usingSystem.Data;8. namespaceWcfService9. 10. ServiceContract11. /XmlSerializerFormat12.
4、 publicinterfaceIService113. 14. OperationContract15. ListGetDataFromListOfDataContract();16. OperationContract17. ListGetDataFromListOfSerializable();18. OperationContract19. DataTableGetDataFromDataTable();20. 21. DataContract22. publicclassCustomerObjectOfDataContract23. 24. DataMember25. publics
5、tringNameget;set;26. DataMember27. publicstringCodeget;set;28. 29. Serializable30. publicclassCustomerObjectOfSerializable31. 32. publicstringNameget;set;33. publicstringCodeget;set;34. 35. 2. 服务实现:c-sharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.Linq;4. usingSys
6、tem.Runtime.Serialization;5. usingSystem.ServiceModel;6. usingSystem.ServiceModel.Web;7. usingSystem.Text;8. usingSystem.Data;9. namespaceWcfService10. 11. publicclassService1:IService112. 13. #regionIService1Members14. publicListGetDataFromListOfDataContract()15. 16. returnnewList17. 18. newCustome
7、rObjectOfDataContractName=test1,Code=aaa,19. newCustomerObjectOfDataContractName=test2,Code=bbb,20. newCustomerObjectOfDataContractName=test3,Code=ccc21. ;22. 23. publicListGetDataFromListOfSerializable()24. 25. returnnewList26. 27. newCustomerObjectOfSerializableName=test1,Code=111,28. newCustomerO
8、bjectOfSerializableName=test2,Code=222,29. newCustomerObjectOfSerializableName=test3,Code=33330. ;31. 32. publicDataTableGetDataFromDataTable()33. 34. vardata=newDataTable(DataTable);35. data.Columns.Add(Name,typeof(string);36. data.Columns.Add(Code,typeof(string);37. data.Rows.Add(newobjectaaa,111)
9、;38. data.Rows.Add(newobjectbbb,222);39. data.Rows.Add(newobjectccc,333);40. returndata;41. 42. 43. #endregion44. 45. 3. 客户端:c-sharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.Linq;4. usingSystem.Text;5. usingSystem.Data;6. namespaceWcfClient7. 8. classProgram9. 10
10、. staticvoidMain(stringargs)11. 12. WcfSvc.Service1Clientclient=newWcfSvc.Service1Client();13. Console.ForegroundColor=ConsoleColor.Yellow;14. Console.WriteLine(ListDataContract:);15. vardata1=client.GetDataFromListOfDataContract();16. foreach(varobjindata1)17. Console.WriteLine(0,1,obj.Name,obj.Cod
11、e);18. Console.WriteLine();19. Console.ForegroundColor=ConsoleColor.Green;20. Console.WriteLine(ListSerializable:);21. vardata2=client.GetDataFromListOfSerializable();22. foreach(varobjindata2)23. Console.WriteLine(0,1,obj.Namek_BackingField,obj.Codek_BackingField);24. Console.WriteLine();25. Consol
12、e.ForegroundColor=ConsoleColor.Red;26. Console.WriteLine(System.Data.DataTable:);27. vardata3=client.GetDataFromDataTable();28. foreach(DataRowrowindata3.Rows)29. Console.WriteLine(0,1,row0,row1);30. Console.WriteLine();31. Console.Read();32. 33. 34. 4. 运行:运行结果说明:WCF除了支持默认的DataContract对象以外,还支持Serial
13、izble对象,DataTable也是被支持的。但是用DataTable作为参数或者返回值时,DataTable的TableName一定不能为空。否则会抛出下面的错误:The underlying connection was closed: The connection was closed unexpectedly.光看这个异常,还真不知道哪不对了。就算你没有调用带DataTable的方法,也不行。很诡异的错误提示。另外,大家注意到这个细节了没有。服务端定义的对象,在客户端生成的代理却是这样的?所有字段都加上了 k_BackingField,原因是默认的DataContractSerial
14、izer序列化搞的鬼DataContract的Response Message XML InfoSetxhtmlview plaincopy1. 2. 3. 4. 5. 6. 7. aaa8. test19. 10. 11. bbb12. test213. 14. 15. ccc16. test317. 18. 19. 20. Serializable的Response Message XML InfoSet,真是面目全非了。xhtmlview plaincopy1. 2. 3. 4. 5. 6. 7. 1118. test19. 10. 11. 22212. test213. 14. 15.
15、 33316. test317. 18. 19. 20. 21. 原因应该是DataContractSerializer(System.Runtime.Serialization命名空间下)是在实际访问方法时,对参数列表的对象进行序列化。标识为DataMember特性的DataContract能够别识别并正常的序列化,而Serializable对象只能通过反射,而被反射出来的Public Field被加进去了不想要的东西。解决办法也简单:就是改用XmlSerializer代替DataContractSerializer。只要在服务契约上声明一下:XmlSerializerFormat特性就可以了。ServiceContractXmlSerializerFormatpublic interface IService1 OperationContract List GetDataFromListOfDataContract(); OperationContract List GetDataFromListOfSeria
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论