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 ()
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)

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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 10545 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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.