Types Supported by the Data Contract Serializer
Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data. The DataContractSerializer is designed to serialize data contract types. However, it supports many other types, which can be thought of as having an implicit data contract. The following is a complete list of types that can be serialized:
All publicly visible types that have a constructor that does not have parameters.
Data contract types. These are types to which the DataContractAttribute attribute has been applied. New custom types that represent business objects should normally be created as data contract types. For more information, see Using Data Contracts and Serializable Types.
Collection types. These are types that represent lists of data. These can be regular arrays of types, or collection types, such as ArrayList and Dictionary<TKey,TValue>. The CollectionDataContractAttribute attribute can be used to customize the serialization of these types, but is not required. For more information, see Collection Types in Data Contracts.
Enumeration types. Enumerations, including flag enumerations, are serializable. Optionally, enumeration types can be marked with the DataContractAttribute attribute, in which case every member that participates in serialization must be marked with the EnumMemberAttribute attribute. Members that are not marked are not serialized. For more information, see Enumeration Types in Data Contracts.
.NET Framework primitive types. The following types built into the .NET Framework can all be serialized and are considered to be primitive types: Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Boolean, Char, Decimal, Object, and String.
Other primitive types. These types are not primitives in the .NET Framework but are treated as primitives in the serialized XML form. These types are DateTime, DateTimeOffset, TimeSpan, Guid, Uri, XmlQualifiedName, and arrays of Byte.
Types marked with the SerializableAttribute attribute. Many types included in the .NET Framework base class library fall into this category. The DataContractSerializer fully supports this serialization programming model that was used by .NET Framework remoting, the BinaryFormatter, and the SoapFormatter, including support for the ISerializable interface.
Types that represent raw XML or types that represent ADO.NET relational data. The XmlElement and array of XmlNode types are supported as a way of representing XML directly. Additionally, types that implement the IXmlSerializable interface are supported, including the related XmlSchemaProviderAttribute attribute, and the XDocument and XElement types. The ADO.NETDataTable type and the DataSet type (as well as its typed derived classes) all implement the IXmlSerializable interface, and therefore fit into this category. For more information, see XML and ADO.NET Types in Data Contracts.
Limitations of Using Certain Types in Partial Trust Mode
The following is a list of limitations when using certain types in partial trust mode scenarios:
When running WCF code in Partial Trust mode, the serialization and deserialization of
private) is not supported. This is because the generated IL is unverifiable and therefore requires elevated permissions.
[DataContract]types must be public.
[DataMember]fields or properties in a
[DataContract]type must be public and read/write. The serialization and deserialization of
readonlyfields is not supported when running WCF in a partially-trusted application.
ISerializable]programming model is not supported in a partial trust environment.
Known types must be specified in code or machine-level configuration (
Machine.config). Known types cannot be specified in application-level configuration for security reasons.
Types that implement IObjectReference throw an exception in a partially-trusted environment because the GetRealObject method requires the security permission
Additional Notes on Serialization
The following rules also apply to types supported by the Data Contract Serializer:
Generic types are fully supported by the data contract serializer.
Nullable value types are fully supported by the data contract serializer.
Interface types are treated either as Object or, in the case of collection interfaces, as collection types.
Both structures and classes are supported.
The DataContractSerializer does not support the programming model used by the XmlSerializer and ASP.NET Web services. In particular, it does not support attributes like XmlElementAttribute and XmlAttributeAttribute. To enable support for this programming model, WCF must be switched to use the XmlSerializer instead of the DataContractSerializer.
The DBNull type is treated in a special way. It is a singleton type, and upon deserialization the deserializer respects the singleton constraint and points all
DBNullreferences to the singleton instance. Because
DBNullis a serializable type, it demands SerializationFormatter permission.