다음을 통해 공유


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:

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:

  • To serialize or deserialize a type that implements ISerializable in partially-trusted code using the DataContractSerializer requires the SerializationFormatter and UnmanagedCode permissions.

  • When running WCF code in Partial Trust mode, the serialization and deserialization of readonly fields (both public and private) is not supported. This is because the generated IL is unverifiable and therefore requires elevated permissions.

  • Both the DataContractSerializer and the XmlSerializer are supported in a partial trust environment. However, use of the DataContractSerializer is subject to the following conditions:

    • All serializable [DataContract] types must be public.

    • All serializable [DataMember] fields or properties in a [DataContract] type must be public and read/write. The serialization and deserialization of readonly fields is not supported when running WCF in a partially-trusted application.

    • The [Serializable]/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 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)].

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 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 DBNull references to the singleton instance. Because DBNull is a serializable type, it demands SerializationFormatter permission.

See Also

Concepts

XML and ADO.NET Types in Data Contracts
Using Data Contracts
Serializable Types
Collection Types in Data Contracts
Enumeration Types in Data Contracts