Deserialize issue with JSON.NET on UWP, using ImmutableList (only Release mode) Deserialize issue with JSON.NET on UWP, using ImmutableList (only Release mode) json json

Deserialize issue with JSON.NET on UWP, using ImmutableList (only Release mode)


Most likely you are hitting one of the runtime exceptions from .NET Native. To properly diagnose this I'd recommend enabling .NET Native compilation for the DEBUG configuration of your project (Project properties > BUILD > Enable .Net Native checkbox). Then, set your debugger to stop on first chance exceptions.

Now when you run you'll probably see a MissingMetadataException or somethign about serialization but because you're in DEBUG you'll get decent exception strings. If you can move to Update 1, the messages are a bit better. Most likely you'll need to add something to your Default.rd.xml file.


In order to reflection-activate ImmutableCollection<T>, Json.NET reflects on ImmutableCollection attempting to find the CreateRange method. Since the .NET Native compiler couldn't predict this due to the use of non-trivial reflection, this lookup fails. The error message coming from Json.NET is quite unfortunate because it doesn't tell you that the inability to reflection-find ImmutableCollection.CreateRange is the real problem.

Now that we know this is the problem, we can use the tool at http://dotnet.github.io/native/troubleshooter/type.html to construct a piece of RD.XML that will fix it. The RD.XML that fixes it is:

<Type Name="System.Collections.Immutable.ImmutableList" Dynamic="Required All" />


We had a similar problem, but the .rd.xml trick did not fix it. The code deserializes the JSON to a dynamic object, and works great in Debug mode, and worked in Release mode prior to updating UWP to 10.0.15138. But now it crashes in Release mode.

Here was the line before the fix (does not work):

dynamic obj = JsonConvert.DeserializeObject(rpcStatus);

The fix is to specify ExpandoObject as the type for Newtonsoft to deserialize to, like so (does work):

dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(rpcStatus);