SignalR 不会发送字节

问题描述 投票:0回答:1

我有 2 条消息:

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);
}

然后从服务器中心调用(ASP.net blazor 应用程序):

    hub.Clients.All.Report(new IClient.ReportPayload {Message = "foo"});
    hub.Clients.All.SetImage(new IClient.SetImagePayload
    {
        ImageTitle = "aaaaaaaa",
        Bytes = bytes
    });

Report()
有效

SetImage()
没有

当我从消息中删除 byte[] 时,

SetImage()
开始工作

我的客户:

    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}");
    });

所以我的结论是 SignalR 无法序列化字节数组

我没有收到任何错误,这很奇怪

c# signalr .net-6.0
1个回答
-1
投票

可以使用signalR的MessagePack协议发送字节数组。

© www.soinside.com 2019 - 2024. All rights reserved.