如何在 C# 中通过 SignalR 传递复杂对象?

问题描述

当我将其更改为(简单)复杂对象(“ CommonMessage”在我的情况下)。从我读到的内容来看,它应该是开箱即用的。 “SendCommonMessage”方法不再被调用,我也没有收到任何错误。我错过了什么?有没有办法调试/获取显示错误? (ASP.NET 服务由 WPF Core 5 应用程序调用。)

public class CommonMessageHub : Hub
{
    public async Task SendCommonMessage(CommonMessage commonMessage)
    {
        await Clients.All.SendAsync("ReceiveCommonMessage",commonMessage);
    }
}

public class CommonMessage
{
    public int MessageType { get; set; }
    public int Id { get; set; }
    public string Text { get; set; }
}

调用方(WPF 应用程序)如下所示:

public class CommonMessageService
{
    public CommonMessageService(HubConnection connection)
    {
        _connection = connection;
        _connection.On<CommonMessage>("ReceiveCommonMessage",commonMessage => CommonMessageReceived?.Invoke(commonMessage));
    }

    private readonly HubConnection _connection;

    public event Action<CommonMessage> CommonMessageReceived;

    public async Task Connect()
    {
        await _connection.StartAsync();
    }

    public async Task SendCommonMessage(CommonMessage commonMessage)
    {
        await _connection.SendAsync("SendCommonMessage",commonMessage);
    }
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)