Search code examples
c#datacontractserializerwindows-10-universal.net-native

DataContractSerialzer errors with CollectionDataContract in UWP


I'm porting existing libraries to the Windows 10 Universal Windows Platform and am having a problem serializing types which are marked with the CollectionDataContract attribute. These classes use the KeyName and ValueName properties of the CollectionDataContract to provide custom names during serialization. Everything works fine in debug mode but fails when the .NET Native Tool Chain is enabled.

For example:

[CollectionDataContract(Name = "OriginalValuesMap",
     ItemName = "OriginalValues", KeyName = "Name", ValueName = "OriginalValue")]
internal class OriginalValuesMap : Dictionary<string, Object> { ... }

When serialized correctly will look something like this:

<OriginalValuesMap>
  <OriginalValues>
     <Name>Company</Name>
     <OriginalValue i:type="d:string">Facebook</OriginalValue>
  </OriginalValues>
</OriginalValuesMap>

Yet in the failing case looks like this:

<OriginalValuesMap>   
   <OriginalValues>
     <b:Key>Company</b:Key>
     <b:Value i:type="d:string">Facebook</b:Value>   
   </OriginalValues> 
</OriginalValuesMap>

Is there a way to work around this with runtime directives?

EDIT1: One workaround I thought would work, using a vanilla CollectionDataContract, also does not work. The names serialized on the .NET Native UWP side do not match what the .NET WCF app is expecting to deserialize.

EDIT2: A workaround which does work is to remove use of the attribute. We do actually want the customized contract names so this isn't ideal.


Solution

  • This issue has been fixed in the latest version. To get the fix, please add an explicit reference to System.Runtime.Serialization.Xml (4.0.11) package from your UWP project. Then rebuild the project and it should work.