Search code examples
serialization.net-corexamarin.formsjson.net.net-standard-2.0

Xamarin Forms - Serialization/Deserialization not working


Expected Behavior:

In the Xamarin.Forms project (MyApp) I am referencing a class library with custom models(Custom.Netcore.Models.dll), contained in a separate project/separate solution. The goal is to serialize the models to json from xml files for our API/Database, retrieve and deserialize them via MyApp's REST using Newtonsoft.Json to fill ViewModel objects, create changes through various interactions via MyApp's UI then serialize them and POST to Custom.Netcore.API.

Actual Behavior:

The expected behavior only functions when running MyApp via UWP. When trying to run the solution via Android/iOS (Emulators) and the application gets to the REST calls to fill MyApp's ViewModels, the Application successfully GETs the serialized data via

var data = await response.Content.ReadAsStringAsync();

then when the application attempts to parse the data and deserialize it to the model via

ObservableCollection<Model> Records = JsonConvert.DeserializeObject<ObservableCollection<Model>>(data);

both Android/iOS at this point give the same error and cause the application to stop running:

Android:

System.TypeLoadException: 'Could not resolve type with token 0100000c from typeref (expected class 'System.Xml.Serialization.XmlRootAttribute' in assembly 'System.Xml.ReaderWriter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')'

iOS:

Failed to resolve "System.Void System.Xml.Serialization.XmlRootAttribute::.ctor()" reference from "System.Xml.ReaderWriter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

however, I am able to successfully create and fill ViewModel objects defined with Custom.Netcore.Models.Model on all platforms via something simple like

ObservableCollection<Model> Records = new ObservableCollection<Model>()
{
    new Model(){ Id = 0, Description = "First Model", Type = "One"},
    new Model(){ Id = 1, Description = "Second Model", Type = "Two"}
};

Logs: (Android)

Can't find custom attr constructor image: /storage/emulated/0/Android/data/com.companyname.MyApp/files/.__override__/Custom.Netcore.Models.dll mtoken: 0x0a000010 due to: Could not resolve type with token 0100000c from typeref (expected class 'System.Xml.Serialization.XmlRootAttribute' in assembly 'System.Xml.ReaderWriter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.Xml.ReaderWriter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Xml.Serialization.XmlRootAttribute member:(null) **System.TypeLoadException:** 'Could not resolve type with token 0100000c from typeref (expected class 'System.Xml.Serialization.XmlRootAttribute' in assembly 'System.Xml.ReaderWriter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')'

Setup:

Custom.Netcore.Api:

  • <TargetFramework>netcoreapp2.1</TargetFramework>
  • Microsoft.AspNetCore.App - v2.1.1
  • Microsoft.NETCore.App - v2.1.0
  • NETStandard.Library - v.2.0.3
  • System.Configuration.ConfigurationManager - v5.0.0-preview.4.20251.6
  • System.Data.SqlClient - v4.8.1
  • System.Runtime - v4.3.1

XF.MyApp:

  • <TargetFramework>netstandard2.0</TargetFramework>
  • Xamarin.Forms - v4.7.0.968
  • Newtonsoft.Json - v12.0.3
  • NETStandard.Library - v2.0.3

Custom.Netcore.Models.Model.cs:

namespace Custom.Netcore.Models
{
    [Serializable()]
    [System.Xml.Serialization.XmlRoot("Model")]
    public class Model
    {
        [System.Xml.Serialization.XmlAttribute("id")]
        public int Id { get; set; }
        [System.Xml.Serialization.XmlAttribute("description")]
        public string Description { get; set; }
        [System.Xml.Serialization.XmlElement("Type")]
        public string Type { get; set; }
    }
}

API Data:

[
  {
    "id": 0,
    "description": "First Model",
    "type" = "One"
  },
  {
    "id": 1,
    "description": "Second Model",
    "type" = "Two"
  }
]

Been stuck trying to workaround this issue for a week so any help would be greatly appreciated!


Solution

  • The logs and errorlist wont directly tell you on iOS and Android, and since I couldnt find a similar issue online I'll leave this for someone in the future. If you are using Xamarin Forms and referencing a Class Library Project in a separate solution be aware that if you get a bad reflection or errors similar to those above, the cause of the issue may be incompatibility between MonoAndroid 9 and NetCore 2.1. The fix for this issue in my case was to use the separate solution NetCore 2.1 version for DB tasks and within the same XF solution, add the same Class Library Project then convert it to target the same NetStandard framework since that's what Xamarin Forms is intended to target.