MissingMethodException Json.Serializer Constructor on type not found exception in WASM unoplatform

问题描述

我正在使用 System.Text.Json.JsonSerializer.Deserialize 将字符串反序列化到我的类中。

这是我的课:

namespace Database
{
    public class Song
    {
        public uint id { get; set; }

        public string name { get; set; }

        public string author { get; set; }

        public int bpm { get; set; }
    }
}

这里是反序列化的代码,jsonData 为 [{"id":1,"name":"1","author":"1","bpm":0},{"id":2,"name":"2","author":"2","bpm":0}]

List<Song> songs = JsonSerializer.Deserialize<List<Song>>(jsonData);

它在 Uno-Platform 中的 UWP 和 Android 项目上运行得非常好。但是,在 WASM 上抛出异常 MissingMethodException。这是日志:

System.MissingMethodException: Constructor on type 'System.Text.Json.Serialization.Converters.listofTConverter`2[[System.Collections.Generic.List`1[[Database.song,BP.Wasm,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]],mscorlib,Version=2.0.5.0,PublicKeyToken=7cec85d7bea7798e],[Database.song,PublicKeyToken=null]]' not found.

据我了解日志。它不知道如何反序列化 List<Song>overview 或任何附加资源都没有说明在什么框架中反/序列化支持什么类型的一些例外情况。

我是否必须为 ListSong 和可能的其他集合定义我自己的转换器?这可能与 Uno 平台有关吗?还是我只是遗漏了一些微不足道的东西?

解决方法

这很可能是由于链接器剥离了所需的代码造成的。打开 WASM 项目中的 LinkerConfig.xml 文件并尝试在那里添加您的程序集/System.Text.Json

<assembly fullname="System.Text.Json" />
<assembly fullname="Database" />