Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using [DataContract] rather than [Serializable] in WCF

Any advantage to using DataContract?

like image 998
user496949 Avatar asked Jan 25 '11 10:01

user496949


People also ask

What is DataContract in WCF?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

What is DataContract serialization?

DataContractSerializer(Type, IEnumerable<Type>) Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.

What is serialization and Deserialization in WCF?

For an introduction to data contracts, see Using Data Contracts. When deserializing XML, the serializer uses the XmlReader and XmlWriter classes. It also supports the XmlDictionaryReader and XmlDictionaryWriter classes to enable it to produce optimized XML in some cases, such as when using the WCF binary XML format.

Which namespace is used for serialization in WCF?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.


1 Answers

See a great comparison of XmlSerializer and DataContractSerializer on Dan Rigsby's blog.

Some points in favor of DataContractSerializer:

  • about 10% faster than XmlSerializer
  • will serialize anything decorated with a [DataMember] - even if it's not public visible
  • will not serialize anything unless you specifically tell it to ("opt-in")
  • you can define the order in which the elements are serialized using the Order= attribute on the [DataMember]
  • doesn't require a parameterless constructor for deserialization
like image 87
marc_s Avatar answered Sep 23 '22 12:09

marc_s