Bug 10545 - DataContractSerializer uses wrong order of members in class hierarchy
Summary: DataContractSerializer uses wrong order of members in class hierarchy
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: master
Hardware: All All
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-02-21 13:40 UTC by Daniel Hilgarth
Modified: 2017-09-06 16:45 UTC (History)
6 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Sample code (1.32 KB, text/plain)
2013-02-21 13:40 UTC, Daniel Hilgarth

Description Daniel Hilgarth 2013-02-21 13:40:23 UTC
Created attachment 3445 [details]
Sample code

According to the MSDN (http://msdn.microsoft.com/en-us/library/ms729813.aspx), the result of serializing an object of a class hierarchy will result in an XML with the members of each level of the hierarchy ordered alphabetically with the members of the top-most class coming first:

"- If a data contract type is a part of an inheritance hierarchy, data members of its base types are always first in the order.
- Next in order are the current type’s data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order.

Mono doesn't take this into account. Instead, Mono orders all members of the complete hierarchy alphabetical.

Steps to reproduce:

Code: See attached file

Compiled with: $> mcs DataContractSerializerBug.cs -r:System.Runtime.Serialization.dll

First run with .NET framework: $> DataContractSerializerBug.exe

<Test.Derived2 xmlns="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><F>F</F><T>T</T><D>D</D><J>J</J><V

As expected: First the members of Base (F, T), then the members of Derived (D, J, V) and finally the members of Derived2

Second run with mono: $> mono DataContractSerializerBug.exe

<Derived2 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/"><A>A</A><D>D</D><E>E</E><F>F</F><G>G</G

Not as expected, the order is incorrect.

This is a real problem, because the ASP.NET Web API - or more likely, the deserialization of DataContractSerializer - requires the members in that specific order.
If that order is not correct, it will silently ignores parts of the posted request and set the corresponding properties to null / default, because it thinks the data was not provided.

There exists a workaround:
1. Add a DataContract attribute to all classes in the hierarchy
2. Annotate all members of the class hierarchy with the DataMember attribute and set the Order property. Important: The order needs to be unique not only in the current class but in all base classes, too.
Comment 1 Daniel Hilgarth 2013-02-21 13:45:46 UTC
Please ignore the small mishap with the sample. I put everything in the class Test instead of only putting the Main in that class. Fixing this doesn't change the result, though.
Comment 2 Bryan Moulton 2013-03-18 20:46:46 UTC
Confirmed this is the case. A web service hosted on a .NET environment (WCF) will not interface properly with Mono clients for this reason (and vice-versa).
Comment 3 Rodrigo Kumpera 2013-05-27 12:18:06 UTC
Martin, could you take a look at this one?

Comment 4 Chris Turner 2014-07-08 18:59:40 UTC
bug 21073 is related to this. I have confirmed in my own testing that if I take the string data generated from a mono datacontractserializer and reorder it according to type, then the normal windows .net datcontractserializer (the one used in webapi) will indeed deserialize correctly.

Note You need to log in before you can comment on or make changes to this bug.