Uno Platform WASM SignalR 反序列化问题
Uno Platform WASM SignalR deserialization issues
我有一个 SignalR 服务和一个 Uno Platform WASM 客户端(带 Prism)。我想调用一个集线器方法,returns 一个模型。问题是,我有两个相同的模型(属性、方法等),但客户端在调用集线器方法时只能接收其中一个。对于另一个模型,反序列化会引发异常。
中心:
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 的调用工作正常,它被设置为服务 returns。对 TestModel 的调用导致异常。如果我切换调用顺序(1.TestModel,2.LocationModel),TestModel 调用仍会抛出异常。
当我为 Uwp 构建时一切正常。
异常:
System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ReservationManagement.SignalRInterface.Model.TestModel'. Path: $ | LineNumber: 0 | BytePositionInLine: 1. ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ReservationManagement.SignalRInterface.Model.TestModel'.
我也试过这个,正如异常提示的那样,使用一个单一的参数化构造函数和一个用 'JsonConstructorAttribute' 注释的参数化构造函数,但仍然是相同的异常。
这是一个与 IL 链接器步骤相关的一般性问题,它会删除检测为未使用的成员,在某些情况下,当使用反射时会错误地删除。
您需要在 WebAssembly 项目的 LinkerConfig.xml
文件中添加链接器描述符来修复此问题,可能在包含 ReservationManagement
命名空间的程序集中。
您可以找到有关 linker configuration here 的其他文档。
我有一个 SignalR 服务和一个 Uno Platform WASM 客户端(带 Prism)。我想调用一个集线器方法,returns 一个模型。问题是,我有两个相同的模型(属性、方法等),但客户端在调用集线器方法时只能接收其中一个。对于另一个模型,反序列化会引发异常。
中心:
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 的调用工作正常,它被设置为服务 returns。对 TestModel 的调用导致异常。如果我切换调用顺序(1.TestModel,2.LocationModel),TestModel 调用仍会抛出异常。
当我为 Uwp 构建时一切正常。
异常:
System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ReservationManagement.SignalRInterface.Model.TestModel'. Path: $ | LineNumber: 0 | BytePositionInLine: 1. ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'ReservationManagement.SignalRInterface.Model.TestModel'.
我也试过这个,正如异常提示的那样,使用一个单一的参数化构造函数和一个用 'JsonConstructorAttribute' 注释的参数化构造函数,但仍然是相同的异常。
这是一个与 IL 链接器步骤相关的一般性问题,它会删除检测为未使用的成员,在某些情况下,当使用反射时会错误地删除。
您需要在 WebAssembly 项目的 LinkerConfig.xml
文件中添加链接器描述符来修复此问题,可能在包含 ReservationManagement
命名空间的程序集中。
您可以找到有关 linker configuration here 的其他文档。