Data Contract (CONTRACT) and Data Contract serializer (DataContractSerializer)
Most of the systems are data-centric, and the realization of the function is handled correctly in the relevant data. And the data itself, is the carrier of effective information, in different environments have different representations. A distributed interconnected system focuses on the exchange of data, and the fundamental premise of data exchange is the consistent understanding of data structure between the two parties involved. This is the performance of the data requirements, in order to ensure that in different platforms, the application of different vendors can be normal data exchange, the exchange of data must be used in a way that everyone can understand. In this context, XML is undoubtedly the best choice. So the serialization of WCF (serialization) solves the problem of how to transform data from the representation of an object into an XML representation to ensure the normal exchange of data. From this chapter, I'll talk about the nature of WCF serialization, first from the data contract.
I. Data contracts
A normal service invocation requires a consistent understanding of service operations by the client and server, and WCF Abstracts Service operations through service contracts, describing services in a way that is not platform-independent and can be understood by different vendors. Similarly, the client and the server for effective data exchange, also requires the exchange between the two parties to the structure of the exchange of consensus, WCF through the data contract to describe the exchange of data. With the definition of the data contract, WCF uses a new serializer-data Contract serializer (DataContractSerializer) to serialize the data contract to the deserialization operation.
Like a service contract, WCF uses attribute-based data contract definitions. The custom attributes based on the data contract mainly include the following two: DataContractAttribute and DataMemberAttribute, and then we'll discuss these two important custom features.
DataContractAttribute and DataMemberAttribute
WCF defines its target type as a data contract by applying the DataContractAttribute attribute, and the following is the definition of DataContractAttribute. From the definition of AttributeUsage, DataContractAttribute can only be used for enumerations, classes, and structs, not interfaces; DataContractAttribute cannot be inherited, That is, when a type inherits an application of the DataContractAttribute attribute type, it only explicitly applies the DataContractAttribute attribute to become a data contract. ; only one DataContractAttribute attribute can be applied on a type.
1: [AttributeUsage (Attributetargets.enum | attributetargets.struct | AttributeTargets.Class, inherited = False, AllowMultiple = False)]
2:public Sealed class Datacontractattribute:attribute
3: {
4:public bool IsReference {get; set;}
5:public string Name {get; set;}
6:public string Namespace {get; set;}
7:}
DataContractAttribute contains only 3 property members. Where name and namespace represent the name and namespace of the data contract; IsReference indicates whether the object's existing reference structure is persisted when serializing. For example, if two attributes of an object refer to an object at the same time, there are two serialization methods, one is to retain the reference structure in the serialized XML, and the other is to serialize the values of two attributes into two separate XML with the same content.
For a service contract, the ServiceContractAttribute that we have applied to an interface or class to define it as a service contract does not mean that the interface or every method member in the class is a service operation. Instead, the corresponding method is defined explicitly as a service operation by OperationContractAttribute. Similarly, the data contract uses this explicit declaration mechanism. For types that have the DataContractAttribute attribute applied, only fields or attribute members that have applied the DataMemberAttribute attribute can become data members of the data contract. The definition of the DataMemberAttribute attribute is shown below.
1: [AttributeUsage (Attributetargets.field | Attributetargets.property, inherited = False, AllowMultiple = False)]
2:public Sealed class Datamemberattribute:attribute
3: {
4:public DataMemberAttribute ();
5:
6:public bool Emitdefaultvalue {get; set;}
7:public bool IsRequired {get; set;}
8:public string Name {get; set;}
9:public int order {get; set;}
10:}
The following list lists the meanings expressed by the 4 attributes of DataMemberAttribute.
Names: The name of the data member, which defaults to the name of the field or property;
Order: The corresponding data member in the final serialization of the location of the XML, the smaller the order value of the former, the default value is-1;
IsRequired: Indicates whether the property member is a mandatory member, and the default value is False, indicating that the member can be defaulted;
Emitdefaultvalue: Indicates whether the value of the data member is equal to the default value and if it needs to be serialized into the final XML, the default value is True, indicating that the default value participates in serialization.
Note: Data contracts and data members are only related to whether DataContractAttribute and DataMemberAttribute are applied, and access restriction modifiers for types and members (public,internal, protected, Private, etc.) has nothing to do. That is, a private field or property member that has applied DataMemberAttribute is also a data member of the data contract.
Second, Data Contract serializer (DataContractSerializer)
In WCF, the definition of a data contract is for serialization and deserialization services. WCF uses the Data Contract serializer (DataContractSerializer) as the default serializer. Next we focus on DataContractSerializer and the serialization rules based on DataContractSerializer. Let's look at the definition of DataContractSerializer.
1:public Sealed class Datacontractserializer:xmlobjectserializer
2: {
3://other Members
4:public DataContractSerializer (type type);
5://Other constructors
6:
7:public Override Object ReadObject (XmlReader reader);
8:public Override Object ReadObject (XmlDictionaryReader reader, bool verifyobjectname);
9:public Override Object ReadObject (XmlReader reader, bool verifyobjectname);
10:public override void WriteObject (XmlWriter writer, object graph);
11:
12:public idatacontractsurrogate datacontractsurrogate {get;}
13:public bool Ignoreextensiondataobject {get;}
14:public readonlycollection<type> knowntypes {get;}
15:public int maxitemsinobjectgraph {get;}
16:public bool Preserveobjectreferences {get;}
17:}