1. 程式人生 > >WCF入門(一)--Request Entity Too large 傳輸的資料量過大

WCF入門(一)--Request Entity Too large 傳輸的資料量過大

      通過WCF進行資料的查詢或者新增的時候,如果資料量過大,一般會報出如下的錯誤:

     1、已超過傳入訊息(65536)的最大訊息大小配額。若要增加配額,請使用相應繫結元素上的MaxReceivedMessageSize 屬性。

     2、遠端伺服器返回了意外反應(413)Request Entity too large。

     3、遠端伺服器返回了意外反應(400)Bad Request。

     具體的解決方案:

     服務端返回資料給客戶端報錯

     在客戶端的配置檔案中,主要修改maxReceivedMessageSize

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_Default" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

     2、客戶端傳資料給服務端報錯

    修改服務端web.config

<system.serviceModel>
    <bindings>
      <basicHttpBinding>                                                                                                       <!--其實要修改所有的服務,不管是服務端還是客戶端,Binding那邊增加一個沒有設定名字的預設配置就OK了-->
        <binding   closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00"
       maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
        </binding >
        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <!-- 為避免洩漏元資料資訊,請在部署前將以下值設定為 false -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- 要接收故障異常詳細資訊以進行除錯,請將以下值設定為 true。在部署前設定為 false 以避免洩漏異常資訊 -->
          <serviceDebug includeExceptionDetailInFaults="True" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1" />
  </system.serviceModel>