WCF 第六章 序列化与编码 比较WCF序列化选项

使用WCF有很多种方式来序列化对象。确定使用哪种方法来序列化取决于一系列因素。这些因素包括你是否想要与契约共享类型,支持现有的.NET类型,保留引用以及更多。

DataContractSerializer

WCF中默认的序列化方法是DataContractSerializer.这个类存在于 System.Runtime.Serialization命名空间里。DataContractSerializer是用来支持基于XSD元数据的契约 共享的。它将公共语言运行时(CLR)类型映射成XSD定义的类型。这意味着XSD是可以用来在两个应用程序间交换数据的公共元数据。例如,你可以使用 XSD在一个.NET应用程序和一个Java应用程序间交换数据。我们使用一个字符串来举个例子。

12-15-2010 3-21-21 PM

    图片6.2 XSD类型

注意只是XSD类型在一个服务端和一个客户端交换,而不是类型信息。所以在图片6.2中,System.String或者 java.lang.String的概念不作为消息交换的一部分。这允许任何一边将XSD类型映射为它们自己环境中的特殊类型。这对于初始类型是可以很好 工作的。复杂类型会变成初始类型的一个扩展。所以我们该如何描述使用DataContractSerializer来将一个.NET CLR类型映射为一个XSD元数据?

  正如在第二章描述的那样,[DataContract]属性可以用来标记一个类型是可序列化的。成员和属性可以使用[DataMember]属性来标记为 数据契约的一部分。当开发人员定义类型是如何被序列化时这是一个非常选择进入的场景。这意味着契约是显式的,与XmlSerializer的非常选择退出 模式不同。列表6.1 显示了一个复杂类型的例子,Employee, 它使用了DataContractSerializer. 我们将使用Employee类型来检查元数据以及使用DataContractSerializer 的序列化输出。这将形成与WCF内部的其他序列化方法比较的基础。

列表6.1 使用DataContractSerialization的Employee类

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Runtime.Serialization;
06 
07namespace Employee
08{
09    [DataContract]
10    public class Employee
11    {
12        private int employeeID;
13        private string firstName;
14        private string lastName;
15 
16        public Employee(int employeeID, string firstName, string lastName)
17        {
18            this.employeeID = employeeID;
19            this.firstName = firstName;
20            this.lastName = lastName;
21        }
22 
23        [DataMember]
24        public int EmployeeID
25        {
26            get { return employeeID; }
27            set { employeeID = value; }
28        }
29 
30        [DataMember]
31        public string FirstName
32        {
33            get { return firstName; }
34            set { firstName = value; }
35        }
36 
37        [DataMember]
38        public string LastName
39        {
40            get { return lastName; }
41            set { lastName = value; }
42        }
43    }
44}

  在列表6.1显示的Employee复杂类型由列表6.2中的XSD元数据表示

列表6.2 Employee XSD 元数据

01<?xml version="1.0" encoding="utf-8"?>
02<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/Employee" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Employee" xmlns:xs="http://www.w3.org/2001/XMLSchema">
03  <xs:complexType name="Employee">
04    <xs:sequence>
05      <xs:element minOccurs="0" name="EmployeeID" type="xs:int" />
06      <xs:element minOccurs="0" name="FirstName" nillable="true" type="xs:string" />
07      <xs:element minOccurs="0" name="LastName" nillable="true" type="xs:string" />
08    </xs:sequence>
09  </xs:complexType>
10  <xs:element name="Employee" nillable="true" type="tns:Employee" />
11</xs:schema>

列表6.3显示了Employee类的元数据是如何导出的。

列表6.3 导出XSD元数据

01class Program
02{
03    static void Main(string[] args)
04    {
05        XsdDataContractExporter xsdexp = new XsdDataContractExporter();
06        xsdexp.Options = new ExportOptions();
07        xsdexp.Export(typeof(Employee));
08 
09        //Write out exported schema to a file
10        using (FileStream fs = new FileStream("sample.xsd", FileMode.Create))
11        {
12            foreach (XmlSchema sch in xsdexp.Schemas.Schemas())
13            {
14                sch.Write(fs);
15            }
16        }
17    }
18}

  为了完成与其他序列化结构比较的基础我们还要做最后一个任务,使用DataContractSerializer来序列化一个Employee实例。列表6.4显示了这是如何完成的。

列表6.4 使用DataContractSerializer序列化

01class Program
02{
03    static void Main(string[] args)
04    {
05        Employee e = new Employee(101, "Daniel", "Dong");
06        FileStream writer = new FileStream("sample.xml", FileMode.Create);
07        DataContractSerializer ser = new DataContractSerializer(typeof(Employee));
08        ser.WriteObject(writer, e);
09        writer.Close();
10    }
11}

  Employee类使用DataContractSerializer的序列化输出结果在列表6.5 中显示。

列表6.5 使用DataContractSerializer的被序列化的Employee类

3  <EmployeeID>101</EmployeeID>
4  <FirstName>Daniel</FirstName>
5  <LastName>Dong</LastName>
6</Employee>


=======

转载自

作者:DanielWise
出处:http://www.cnblogs.com/danielWise/
 

posted @ 2011-06-30 10:09  Gavin Liu  阅读(271)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!