I am testing an app in Release mode and it fails when serializing my data with InvalidDataContractException citing SerializationCodeIsMissingForType.
By a process of elimination the objects that fail serialisation in Release (not Debug) mode are Row[], Column[] which are just arrays of simple classes.
I have managed to serialise my other objects by adding Default.rd.xml entries like:
<Namespace Name="Windows.UI">
<TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
</Namespace>
I'm stumped trying to come up with a correct TypeInstantiation entry for System.Array as I don't know what to use for the mandatory Arguments parameter.
Can anyone help me, or am I on the wrong track completely?
Thanks...
Robert
I managed to get it working by much trial and error. Obviously everything had been build and tested in Debug mode so there was a list of objects being serialised that failed a Release build with SerializationCodeIsMissingForType.
To help others (as there aren't too many examples out there yet) this was my journey.
My serialisation code was as follows:
persistantData = new Dictionary<string, object>();
persistantData.Add(nameof(Version), Version);
persistantData.Add(nameof(FileVersion), FileVersion);
persistantData.Add(nameof(ScrollParameters), ScrollParameters);
persistantData.Add(nameof(DefaultCellBackgroundColour), DefaultCellBackgroundColour);
persistantData.Add(nameof(Columns), Columns);
persistantData.Add(nameof(Rows), Rows);
persistantData.Add(nameof(Cells), listOfCellData);
persistantData.Add(nameof(Filename), FileName);
private async Task SaveTheFileAsync(StorageFile file)
{
using (MemoryStream memoryStream = new MemoryStream())
{
var serializer = new DataContractSerializer(typeof(Dictionary<string, object>));
serializer.WriteObject(memoryStream, persistantData); //Failed here
...etc
First I commented out all .Add()s above with the exception of the first. The same SerializationCodeIsMissingForType exception occurred until I added the following to the standard Default.rd.xml file in my project's Properties to define the Dictionary collection type:
<Namespace Name="System.Collections.Generic">
<TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" />
</Namespace>
Adding the Version object back required this to be added to Default.rd.xml (Version is of type PackageVersion).
<Namespace Name="Windows.ApplicationModel">
<TypeInstantiation Name="PackageVersion" Arguments="System.UInt16, System.UInt16, System.UInt16, System.UInt16" Dynamic="Required All" DataContractSerializer="Required All" />
</Namespace>
Adding FileVersion worked immediately as its type is int. Likewise ScrollParameters as it is a class containing two doubles and a float.
DefaultCellBackgroundColour needed this in Default.rd.xml:
<Namespace Name="Windows.UI">
<TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
</Namespace>
Columns, Rows and Cells required separate Type entries in Default.rd.xml:
<Type Name="Columns" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="Rows" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="CellData" Dynamic="Required All" DataContractSerializer="All" />
But because CellData was a List collection that type needed to be added to System.Collections.Generic namespace entry (after Dictionary) as follows:
<Namespace Name="System.Collections.Generic">
<TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" DataContractSerializer="Required All" />
<TypeInstantiation Name="List" Arguments="CellData" Dynamic="Required All" DataContractSerializer="All" />
</Namespace>
Also the CellData definition contained mostly strings, integers, booleans and decimals but one property is defined as TextAlignment. The latter needed its own entry in Default.rd.xml like this:
<Namespace Name="Windows.UI.Xaml">
<TypeInstantiation Name="TextAlignment" Arguments="System.Enum" Dynamic="Required All" DataContractJsonSerializer="Required All"/>
</Namespace>
Finally FileName (a string) required no additional Default.rd.xml entry and it all worked.
Thanks to rbr94 for trying to help. The heading of this question is now misleading as Array definitions are not required.
My complete Default.rd.xml file is below
Hope this helps someone, sometime...
Robert
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<!--
An Assembly element with Name="*Application*" applies to all assemblies in
the application package. The asterisks are not wildcards.
-->
<Assembly Name="*Application*" Dynamic="Required All" />
<!-- Add your application specific runtime directives here. -->
<!-- Make all members of a type visible to .NET Native -->
<Type Name="Columns" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="Rows" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="CellData" Dynamic="Required All" DataContractSerializer="All" />
<Namespace Name="System.Collections.Generic">
<TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" DataContractSerializer="Required All" />
<TypeInstantiation Name="List" Arguments="CellData" Dynamic="Required All" DataContractSerializer="All" />
</Namespace>
<Namespace Name="Windows.ApplicationModel">
<TypeInstantiation Name="PackageVersion" Arguments="System.UInt16, System.UInt16, System.UInt16, System.UInt16" Dynamic="Required All" DataContractSerializer="Required All" />
</Namespace>
<Namespace Name="Windows.UI">
<TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
</Namespace>
<Namespace Name="Windows.UI.Xaml">
<TypeInstantiation Name="TextAlignment" Arguments="System.Enum" Dynamic="Required All" DataContractJsonSerializer="Required All"/>
</Namespace>
</Application>
</Directives>