Loading ...

A short note on WCF Data Contract and the Serialization attribute | CodeAsp.Net

A short note on WCF Data Contract and the Serialization attribute

 /5
0 (0votes)

While serializing your WCF data items, we have two options:

1. Use the [DataContract] attribute: new attribute available in WCF (.NET 3.0 or above). Used with [DataMember] and [DataContractSerializer]

2. Use the [Serializable] attribute: older attribute, required when using the BinaryFormatter class.

Usages and differences

Though both attributes can be used together, the [Serializable] or XmlSerializer allows more flexiblity when dealing with XML whereas the DataContractSerializer gives you a lot more control over the code. Note that some schema elements are not supported by the DataContractSerializer, for example the xs:choice element.

Secondly, the DataContractSerializer can serialize object member fields whereas the XmlSerializer needs properties to be defined for each member field.

By default, WCF uses DataContractSerializer, as it is more efficient than XmlSerializer because it doesnt allow you to have full control over the serialization process, and hence can do its own optimizations. But the main disadvantage is that you need to send parameters alphabetically (or in a particular sort order) if you are using DataContractSerlializer. This can be a problem for clients consuming your WCF REST website as they will have to send the POST data in a particular format. For such cases, it is best to use XmlSerializer.

Comments (no comments yet)

Top Posts