Uno 平台 WASM SignalR 反序列化问题

问题描述

我有一个 SignalR 服务和一个 Uno Platform WASM 客户端(带有 Prism)。我想调用一个集线器方法,它返回一个模型。问题是,我有两个相同的模型(属性方法等),但是客户端在调用集线器方法时只能接收其中一个。对于其他模型,反序列化会引发异常。

中心:

using Microsoft.AspNetCore.SignalR;
using ReservationManagement.SignalRInterface.Model;
using System.Threading.Tasks;

namespace ReservationManagement.Service.Hubs
{
    public class TestServiceHub: Hub
    {
        public async Task<LocationModel> LocationModel()
        {
            return new LocationModel() { Name = "Location" };
        }

        public async Task<TestModel> TestModel()
        {
            return new TestModel() { Name = "Test" };

        }
    }
}

型号:

namespace ReservationManagement.SignalRInterface.Model
{
    public class TestModel
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}


namespace ReservationManagement.SignalRInterface.Model
{
    public class LocationModel
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}

视图模型:

using Microsoft.AspNetCore.SignalR.Client;
using Prism.Commands;
using ReservationManagement.SignalRInterface.Model;

namespace ReservationManagement.Unoprism.viewmodels
{
    class Testviewmodel: viewmodelBase
    {

        private HubConnection HubConnection { get; set; }

        public DelegateCommand Loaded { get; private set; }


        public LocationModel LocationModel
        {
            get => _locationModel;
            set { SetProperty(ref _locationModel,value); }
        }

        private LocationModel _locationModel;

        public TestModel TestModel
        {
            get => _testModel;
            set { SetProperty(ref _testModel,value); }
        }

        private TestModel _testModel;

        public Testviewmodel()
        {
            Loaded = new DelegateCommand(LoadedExecute);
        }

        private async void LoadedExecute()
        {


            HubConnection = new HubConnectionBuilder()
                .WithUrl("http://localhost:5000/TestServiceHubAnyOrigin")
                .WithAutomaticReconnect()
                .Build();

            await HubConnection.StartAsync();

            LocationModel = await HubConnection.InvokeAsync<LocationModel>("LocationModel");
            TestModel = await HubConnection.InvokeAsync<TestModel>("TestModel");
        }
    }
}

对 LocationModel 的调用工作正常,并且设置为服务返回的内容。对 TestModel 的调用导致异常。如果我切换调用顺序 (1. TestModel,2. LocationModel),TestModel 调用仍会抛出异常。

当我为 Uwp 构建时,一切正常。

异常:

System.NotSupportedException:不支持对没有无参数构造函数、单一参数化构造函数或用“JsonConstructorAttribute”注释的参数化构造函数的类型进行反序列化。输入“ReservationManagement.SignalRInterface.Model.TestModel”。路径:$ |行号:0 | BytePositionInLine: 1. ---> System.NotSupportedException: 不支持对没有无参数构造函数、单一参数化构造函数或用“JsonConstructorAttribute”注释的参数化构造函数的类型进行反序列化。输入“ReservationManagement.SignalRInterface.Model.TestModel”。

我也试过这个,正如异常所暗示的那样,使用单个参数化构造函数一个用“JsonConstructorAttribute”注释的参数化构造函数,但仍然是相同的异常。

解决方法

这是一个与 IL 链接器步骤相关的通用问题,该步骤删除了检测为未使用的成员,在某些情况下,在使用反射时会错误地删除。

您需要在 WebAssembly 项目的 LinkerConfig.xml 文件中添加链接器描述符来解决此问题,可能在包含 ReservationManagement 命名空间的程序集中。

您可以找到有关 linker configuration here 的其他文档。