Created attachment 3445 [details]
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.
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.
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).
Martin, could you take a look at this one?
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.