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.

  1. CLR built-in types
  2. Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array [This includes XElement and XNode array from .NET 3.5]
  3. Enums
  4. Types marked with DataContract or CollectionDataContract attribute
  5. Types that implement IXmlSerializable
  6. Arrays and Collection classes including List<T>, Dictionary<K,V> and Hashtable.
  7. Types marked with Serializable attribute including those that implement ISerializable.
  8. Types with none of the above attributes (POCO) but with a default constructor (can be non-public). [This is supported only from .NET 3.5 SP1]

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<System.Uri> uriList;

    [DataMember]

    System.Data.SqlTypes.SqlInt32 sqlInt;

    [DataMember]

    System.Drawing.KnownColor knownColor;

}