You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SignalR无法发送含byte[]的消息问题求助

SignalR无法发送含byte[]的消息问题求助

大家好,我在ASP.NET Blazor项目里用SignalR遇到了一个棘手的问题,想请各位帮忙看看。

先给大家看下我的相关代码:

定义的客户端接口

public interface IClient
{
    public struct ReportPayload
    {
        public string Message { get; set; }
    }
    Task Report(ReportPayload payload);

    public struct SetImagePayload
    {
        public string ImageTitle { get; set; }
        public byte[] Bytes { get; set; }
    }
    Task SetImage(SetImagePayload payload);
}

服务端Hub的调用代码

// 调用Report方法
hub.Clients.All.Report(new IClient.ReportPayload { Message = "foo" });

// 调用SetImage方法
hub.Clients.All.SetImage(new IClient.SetImagePayload
{
    ImageTitle = "aaaaaaaa",
    Bytes = bytes
});

客户端连接与监听代码

Connection = new HubConnectionBuilder()
    .WithUrl($"http:localhost:1234/api")
    .WithAutomaticReconnect()
    .Build();

Connection.On<IClient.ReportPayload>(nameof(IClient.Report), payload =>
{
    Console.WriteLine($"Server has reported: {payload.Message}");
});

Connection.On<IClient.SetImagePayload>(nameof(IClient.SetImage), payload =>
{
    Console.WriteLine($"SetImage got the message: {payload.Bytes.Length}");
});

问题现象

  • Report()方法完全正常工作,客户端能收到消息并输出日志
  • SetImage()方法却完全没反应,客户端根本收不到消息
  • 但如果我把SetImagePayload里的byte[] Bytes字段去掉,这个方法就又能正常工作了

我自己推测是SignalR在序列化byte[]的时候出了问题,但奇怪的是全程没有任何错误提示,这让我排查起来特别头疼。有没有大佬遇到过类似的问题,或者能给我一些排查方向?

备注:内容来源于stack exchange,提问作者caxapexac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 09:28:11