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

如何修改SignalR最大消息大小?大JSON消息传输受限求助

没问题!在SignalR里调整大型JSON消息的传输上限完全可行,分服务器端和客户端两边配置就搞定了,我给你把步骤说清楚:

服务器端配置

不管你用的是.NET 6+的顶级语句模式(Program.cs)还是老版本的Startup.cs,都能在注册SignalR服务的时候调整最大消息大小:

  • .NET 6+ Program.cs
builder.Services.AddSignalR(options =>
{
    // 这里设置为10MB,单位是字节,你可以根据需求改
    options.MaximumReceiveMessageSize = 10 * 1024 * 1024;
});
  • 老版本Startup.cs
    在ConfigureServices方法里添加:
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR(options =>
    {
        options.MaximumReceiveMessageSize = 10 * 1024 * 1024; // 10MB
    });
}

如果只想给某个特定的Hub单独设置上限,也可以在映射Hub的时候单独配置:

app.MapHub<YourTargetHub>("/your-hub-endpoint", options =>
{
    options.MaximumReceiveMessageSize = 15 * 1024 * 1024; // 给这个Hub设15MB
});
客户端配置

光调服务器端还不够,客户端默认也有消息大小限制,得同步调整:

JavaScript客户端

创建HubConnection后直接设置maximumReceiveMessageSize:

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/your-hub-endpoint")
    .configureLogging(signalR.LogLevel.Information)
    .withAutomaticReconnect()
    .build();

// 同样设为10MB,和服务器端保持一致更稳妥
connection.maximumReceiveMessageSize = 10 * 1024 * 1024;

.NET客户端

构建HubConnection时通过属性设置:

var connection = new HubConnectionBuilder()
    .WithUrl("https://your-server-domain/your-hub-endpoint")
    .ConfigureLogging(logging => logging.AddConsole())
    .Build();

// 调整最大接收消息大小
connection.MaximumReceiveMessageSize = 10 * 1024 * 1024;
// 可选:如果消息传输耗时久,顺便调大超时时间
connection.ServerTimeout = TimeSpan.FromMinutes(5);
connection.HandshakeTimeout = TimeSpan.FromMinutes(5);

最后提醒一句:设置的大小要符合实际业务需求,别盲目调得太大,不然可能会影响服务器的内存和性能哦~

内容的提问来源于stack exchange,提问作者Kamran Kia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:53:06