微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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 举报,一经查实,本站将立刻删除。