Friday, July 18, 2008

WCF Serialization Programming Model

WCF Serialization Programming Model

DataContract is the default serialization programming model for WCF. However WCF supports more than just the types marked wth DataContract attribute. It supports serialization of the following kinds of types in the default mode.

CLR built-in types
Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array
Enums
Types marked with DataContract or CollectionDataContract attribute
Types that implement IXmlSerializable
Arrays and Collection classes including List, Dictionary and Hashtable.
Types marked with Serializable attribute including those that implement ISerializable.
Some types may implement more than one of the above programming model. In such cases a programming model is chosen based on its priority as given by the above list. For example, Hashtable is a collection class but also implements ISerializable and it is serialized as a collection type. DataSet implements both IXmlSerializable and ISerializable and it is serialized as IXmlSerializable.



Here is a DataContract class that no one will ever see in real world. Figuring out which programming model is implemented by these types is left as an exercise to the readers.



[DataContract]
public class ComplexDataContract
{
[DataMember]
string name;

[DataMember]
System.Version version;

[DataMember]
System.IO.FileInfo fileInfo;

[DataMember]
System.Collections.Generic.List uriList;

[DataMember]
System.Data.SqlTypes.SqlInt32 sqlInt;

[DataMember]
System.Drawing.KnownColor knownColor;
}

No comments: