Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF DataContract DataMember order?

Tags:

Is the xml that is created from your DataContract created in alphabetical order. I have a DataContract class defined as:

[DataContract(Name = "User", Namespace = "")] public class User {     [DataMember]     public string FirstName { get; set; }     [DataMember]     public string LastName { get; set; }     [DataMember]     public string Email { get; set; }     [DataMember]     public string Password { get; set; }  } 

When I did the following POST:

<User>    <FirstName>abc</FirstName>    <LastName>123</LastName>    <Email>[email protected]</Email>    <Password>pass</Password> </User> 

When I did a GET after my post and returned the result as JSON, email was null, but if I POST my xml as:

 <User>    <Email>[email protected]</Email>    <FirstName>abc</FirstName>    <LastName>123</LastName>    <Password>pass</Password>  </User> 

Email is no longer null when I do a GET and return it as JSON. Why is it doing this?

like image 721
Xaisoft Avatar asked Sep 28 '10 19:09

Xaisoft


People also ask

What is DataContract and DataMember in WCF?

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.

Is DataContract mandatory in WCF?

No, the DataContractAttribute is not required - WCF will infer serialization rules.

Why DataContract is used 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 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.


1 Answers

decorate it with the Order Parameter in the DataMemberAttribute class:

[DataMember(Order = index)] 

The reflector in the serializer puts it alphabetically. Unless when decorated like this:

[DataMember(Order = 0)] public string FirstName { get; set; } [DataMember(Order = 1)] public string LastName { get; set; } [DataMember(Order = 2)] public string Email { get; set; } [DataMember(Order = 3)] public string Password { get; set; } 

Read more here...

like image 163
Caspar Kleijne Avatar answered Sep 30 '22 18:09

Caspar Kleijne