WCF Learning (iii): Data contract 1

Source: Internet
Author: User
Tags net serialization object serialization reflection serialization

WCF can host CLR types, and clients can pass and process CLR-type data (such as string and int), but if we define the type ourselves (for example, the customer class declared). In fact, WCF's passing the CLR's own type is because it has been serialized into an XML information set, and the type we define ourselves needs to be serialized by the Declaration itself.

Serialization of

. NET Serialization.. NET is the automatic implementation of object serialization and deserialization through reflection mechanism (reflection mechanism reference document). First. NET is able to capture the value of each field of an object, then serialize, and when deserialized,. NET creates a new object of the corresponding type, reads the persisted value, and then sets the value of the field. The serialization of the. NET object state to the stream.

Our custom class or struct does not support serialization, so if our class is to be serialized it needs to be specified by the developer of the class. If you need to support serialization, we can add the serializable property to the class or struct.

Such as:

[Serializable]
public class Customer
{
...
}

But if you add serializable to require members of the type to support serialization, it throws an exception, but we can specify that the member does not need serialization by adding the Nonserizlized property to the member.

Format device

. NET provides two types of formatters for serialization and deserialization:

1.BinaruFormatter: Serializes the type into binary format.

2.SoapFormatter: Formatted as SOAP-specific XML format.

But both have to persist the type of assembly and versioning information into the stream, and service-oriented requirements participants can have type assemblies, so if you have to share the stream in both formats, this is certainly not our ideal. This has the WCF format device.

WCF's formatter: DataContractSerializer, is the ability to share data contracts. When WCF data is serialized, WCF typically invokes the DataContractSerializer sequence itself, without the need for us to specify. We can also use DataContractSerializer to serialize the stream.

Data Contract characteristics

Serializable requires that all members of the type be serializable and do not support aliases of type names and member names, so that we cannot explicitly specify which members are to be placed in the data contract. However, WCF data contract features DataContract solve these problems well.

The DataContract Attribute is in the namespace System.Runtime.Serialization, can be identified on the enum, Struct, class. Have member Name,namespace. WCF does not serialize a member of a type when we are using it on a class or struct. Such as:

[DataContract]
public class Emloyee
{public
string _firstname;
public string _lastname;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.