天天看點

WCF錯誤:413 Request Entity Too Large

在我們用WCF傳輸資料的時候,如果啟用預設配置,傳輸的資料量過大,經常會出這個錯誤。

WCF包含服務端與用戶端,是以這個錯誤可能出現在服務端傳回資料給用戶端,或用戶端傳資料給服務端時。

1. 服務端傳回資料給用戶端報錯

在用戶端配置檔案中,主要是配置maxReceivedMessageSize

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceProxyBinding" closeTimeout="00:10:00" receiveTimeout="00:10:00"
          sendTimeout="00:10:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="134217728" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:5239/AHMTService/PartService.svc"
        binding="basicHttpBinding" bindingConfiguration="ServiceProxyBinding"
        contract="UniCloud.ServiceContracts.IPartService" name="IPartService" />
    </client>
  </system.serviceModel>      

2 用戶端傳資料給服務端報錯

 修改服務端web.config,主要也是配置maxReceivedMessageSize

<system.serviceModel>
    <services>
      <!--DecodeService 服務端配置 增加接收檔案大小-->
      <service name="UniCloud.Services.DecodeService" behaviorConfiguration="MyServiceBehaviour" >
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" contract="UniCloud.ServiceContracts.IDecodeService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5239/AHMTService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="MyServiceBinding"  closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None"></security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <!-- 為避免洩漏中繼資料資訊,請在部署前将以下值設定為 false -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- 要接收故障異常詳細資訊以進行調試,請将以下值設定為 true。在部署前設定為 false 以避免洩漏異常資訊 -->
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>      

其實要修改所有的服務,不管是服務端還是用戶端,Binding那邊增加一個沒有設定名字的預設配置就OK了

<binding   closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">      

如果我的文章對你有幫助,就點一下推薦吧.(*^__^*)

wcf

繼續閱讀