Search code examples
.netwcfserializationattributesdatacontract

What is the advantage of using DataContractAttribute over SerializableAttribute?


I have found that my WCF services work normally when the data types involved doesn't have the [DataContract], but the [Serializable] instead.

But all the WCF tutorials shows the first one instead of the latter. Why?


Solution

  • DataContractAttribute gives you more control over what gets sent over the wire, so you can opt to only send the necessary fields of a given entity. Serializable uses platform serialization, which assumes .NET and the same (or similar) versions of the types on both ends of the wire- it (usually) serializes all the private members, state, etc. DCS is intended for a lightweight XML-ish representation that you can have some control over, and XmlSerializer is for an XML format that you can have very fine control over (attribute data, etc).