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
相关产品推荐
相关产品推荐

