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

WCF服务遇413请求过大与400错误,请求协助排查

排查与修复步骤

1. 绑定类型不匹配是核心问题

你提到服务是基于JSON的WCF服务,但当前配置用的是basicHttpBinding——这是SOAP协议绑定,默认不处理JSON请求,客户端发送的JSON格式请求会被服务端判定为无效,直接返回400 Bad Request。你需要改用webHttpBinding来配置REST风格的JSON服务,修改后的配置示例如下:

<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="MyWebHttpBinding" 
               maxReceivedMessageSize="2147483647"
               maxBufferPoolSize="2147483647"
               maxBufferSize="2147483647">
        <!-- 大图片传输必须配置readerQuotas,否则会因数据元素超限报错 -->
        <readerQuotas maxArrayLength="2147483647"
                      maxStringContentLength="2147483647"
                      maxBytesPerRead="2147483647"
                      maxDepth="2147483647"
                      maxNameTableCharCount="2147483647"/>
      </binding>
    </webHttpBinding>
  </bindings>
  <services>
    <service name="StampTrackerServiceLibrary.StampTrackerService">
      <endpoint address="http://LocalHost:9091/StampTrackerService" 
                binding="webHttpBinding"
                bindingConfiguration="MyWebHttpBinding"
                contract="StampTrackerServiceLibrary.IStampTrackerServiceLibrary"
                behaviorConfiguration="JsonBehavior"/>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="JsonBehavior">
        <webHttp defaultOutgoingResponseFormat="Json" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

2. 检查契约与服务名称准确性

  • 确认service name完全匹配服务类的完整命名(命名空间+类名),比如StampTrackerServiceLibrary.StampTrackerService是否和代码中的类名一致。
  • 确认contract完全匹配接口的完整命名,避免拼写错误或命名空间遗漏。

3. 同步客户端配置

  • 若使用WinForm的WCF代理客户端,客户端app.config需同步使用webHttpBinding,并设置相同的maxReceivedMessageSize和readerQuotas参数。
  • Android客户端需确保请求头设置Content-Type: application/json,请求体为标准JSON格式。

4. 若坚持使用SOAP协议(仅适用于SOAP客户端)

如果你的服务实际是SOAP协议,只是误提了JSON,需补充readerQuotas配置,否则大图片的二进制数据会因数组长度超限触发400错误:

<basicHttpBinding>
  <binding name="MyBasic" 
           maxReceivedMessageSize="2147483647" 
           maxBufferPoolSize="2147483647" 
           maxBufferSize="2147483647">
    <readerQuotas maxArrayLength="2147483647"
                  maxStringContentLength="2147483647"
                  maxBytesPerRead="2147483647"
                  maxDepth="2147483647"
                  maxNameTableCharCount="2147483647"/>
  </binding>
</basicHttpBinding>

5. 验证服务启动状态

启动控制台调试程序后,用浏览器访问端点地址,确认服务正常监听(REST服务会返回帮助页面或格式提示,不会直接返回400),排除端口占用或地址配置错误问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 11:57:26