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

求助:提升Service Fabric中托管Web API的默认消息大小限制

调整Service Fabric中Web API的消息大小限制

没问题,我帮你搞定Service Fabric里Web API消息大小限制的调整!虽然确实不建议直接传10MB这么大的对象,但既然业务需求摆在这,咱们直接上针对性的解决方案:

针对ASP.NET Core 版本的Service Fabric Web API

如果你用的是ASP.NET Core构建的Web API服务,需要在两个关键地方配置:

  1. 配置Kestrel服务器的请求大小上限
    在你的Program.cs里,添加Kestrel的限制配置,直接指定最大请求体大小为10MB:
builder.WebHost.ConfigureKestrel(options =>
{
    // 设置最大请求体大小为10MB(单位是字节)
    options.Limits.MaxRequestBodySize = 10 * 1024 * 1024;
});
  1. 配置MVC的输入格式化器限制
    在添加Controllers的配置中,调整JSON输入格式化器的最大请求大小,避免MVC层面截断请求:
builder.Services.AddControllers()
    .AddMvcOptions(options =>
    {
        foreach (var formatter in options.InputFormatters)
        {
            if (formatter is SystemTextJsonInputFormatter jsonInputFormatter)
            {
                // 同样设置为10MB
                jsonInputFormatter.MaxRequestBodySize = 10 * 1024 * 1024;
            }
        }
    });

做完这些配置后,记得重启你的Service Fabric服务或者重新部署应用,让新配置生效。


针对.NET Framework 版本的Service Fabric Web API

如果是基于.NET Framework的传统Web API服务,需要修改Web.config文件:

  1. 调整httpRuntime的请求长度限制
    在system.web节点下添加maxRequestLength,单位是KB(10MB=10240KB):
<system.web>
    <httpRuntime targetFramework="4.8" maxRequestLength="10240" />
</system.web>
  1. 配置webHttpBinding的消息大小限制
    在system.serviceModel节点下,定义支持大消息的绑定配置,并关联到你的服务端点:
<system.serviceModel>
    <bindings>
        <webHttpBinding>
            <binding name="LargeMessageBinding"
                     maxReceivedMessageSize="10485760" <!-- 10MB,单位字节 -->
                     maxBufferSize="10485760"
                     maxBufferPoolSize="10485760">
                <readerQuotas maxDepth="64"
                              maxStringContentLength="10485760"
                              maxArrayLength="10485760"
                              maxBytesPerRead="10485760"
                              maxNameTableCharCount="10485760" />
            </binding>
        </webHttpBinding>
    </bindings>
    <services>
        <service name="YourServiceNamespace.YourWebApiService">
            <endpoint address="" 
                      binding="webHttpBinding" 
                      bindingConfiguration="LargeMessageBinding" 
                      contract="YourServiceNamespace.IYourWebApiContract" 
                      behaviorConfiguration="WebHttpBehavior" />
        </service>
    </services>
</system.serviceModel>

注意替换上面代码中的服务命名空间、服务名称和契约名称为你实际的内容。


额外注意点

  • 如果你的Service Fabric集群使用了反向代理,也要检查反向代理的配置,确保它的消息大小限制不小于10MB,避免请求在反向代理环节被截断。
  • 虽然当前业务必须传大对象,还是建议后续优化:比如采用分块上传、或者先把大对象上传到Blob存储,再给API传存储地址的引用,这样能提升性能和稳定性。

内容的提问来源于stack exchange,提问作者Manas Singhal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:16:34