WCF 请求正文过长导致返回400错误解决方案

今天用WCF做文章新增的时候,发现当POST请求正文超过一定字符数量(ContentLength > 1w)后服务器总是返回400错误,最后查明是由于WCF对请求正文长多做了限制,可以在配置文件中加入如下配置。

<system.serviceModel>
    <services>
      <service behaviorConfiguration="webApi.AddBehavior" name="webApi.Add">
        <host>
          <baseAddresses >
            <add baseAddress="http://localhost:8000/api/"></add>
          </baseAddresses>
        </host>
        <endpoint address="add" binding="webHttpBinding" bindingConfiguration="webBind" behaviorConfiguration="restful" contract="webApi.IAdd"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="webApi.AddBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restful">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webBind" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

添加对绑定的配置,设定可以接受的正文的最大长度.


 <bindings>
      <webHttpBinding>
        <binding name="webBind" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>


在终结点中添加绑定配置的name

bindingConfiguration="webBind"

你可能感兴趣的:(REST,错误,WCF)