Search code examples
c#exceptionserializationuwpdatacontractserializer

UWP DataContractSerializer ReadObject throws System.TypeInitializationException


I have tested it even on empty classes. Serialization is going fine, but when I call ReadObject method, it thorws System.TypeInitializationException. Details:

System.TypeInitializationException: 'The type initializer for 'System.Runtime.Serialization.XmlFormatReaderGenerator' threw an exception.'

Inner Exception:

InvalidOperationException: The API 'System.Runtime.Serialization.FormatterServices.GetUninitializedObject(System.Type)' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.

I searched for it on SO and whole Google, but no success.

Target of project: Windows 10 Anniversary Edition (10.0; Build 14393)


Solution

  • The 'System.Runtime.Serialization.FormatterServices' relevant APIs are the new features which are introduced from .NET Standard 2.0. Please check the .NET API Browser

    In order to use .NET Standard 2.0 in UWP, you need to target Fall Creators Update (FCU) as the minimum version of your UWP project. Please read Announcing UWP Support for .NET Standard 2.0 for more information.