Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContracts and DataMembers

Are there any ways to tell WCF to serialize the whole class when it returns? Do I literally have to add DataMember to every property?

like image 289
zachary Avatar asked Apr 15 '11 20:04

zachary


People also ask

What is Datacontract and DataMember?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What does Datacontract mean?

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 DataMember in C#?

Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute.

What is Datacontract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.

What is EmitDefaultValue?

EmitDefaultValue. DataMember EmitDefaultValue is a Boolean attribute with the default value of true. If the value is not provided for DataMember then the corresponding default value will be set to the member for example integer it will be set as 0, for bool it is false, any reference type is null.


2 Answers

Since .NET 3.5 SP1, you don't have to do this anymore.

If you don't have any [DataContract] and [DataMember] attributes, the DataContractSerializer class will behave like the old XmlSerializer: it will serialize all public read/write properties that are listed in the class.

You do lose a few things in the process though:

  • since you don't have [DataMember] attributes, you cannot define an order of the fields anymore - they'll be serialized in the order of appearance

  • you cannot omit a public property (since that would require [DataMember] on all other properties/fields)

  • you cannot define a property to be Required (that would be on the [DataMember] attribute again)

  • your class now needs to have a public, parameter-less constructor (usually not needed for data contracts)

Read all about it in detail from Aaron Skonnard at Pluralsight.

like image 189
marc_s Avatar answered Sep 19 '22 17:09

marc_s


I love marc's answer, but I want to add some more info.

DataContractSerializer and DataContractJsonSerializer both support, out of the box, many other serialization models as well. This includes IXmlSerializable, Serializable, and ISerializable. POCO support was added in .NET 3.5 SP1, but support for these other models has always been there since .NET 3

This blog post details the extent of the support and more importantly, the prioritization of different models by the serializer (i.e., it tells you what DataContract-based serializers would do if you have one type decorated with multiple serialization models)

So, if you read that blog post, you'll notice that POCO support is last in the priority list. It is the serializer's last resort, if there is absolutely no other serialization programming model available on the type or its parent. For example, if the type is an enumerable of some sort, it will get serializaed according to traditional collection rules. If it's ISerializable or Serializable, it will get serialized according to their serialization rules.

Another important difference: during deserialization of all other types, the default zero-params constructor is never called. For POCO types, it is always called! This gives you an additional hook you don't have so easily in other serialization models!

like image 20
krisragh MSFT Avatar answered Sep 18 '22 17:09

krisragh MSFT