WCF服务端返回:(413) Request Entity Too Large

Posted lance的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WCF服务端返回:(413) Request Entity Too Large相关的知识,希望对你有一定的参考价值。

出现这个原因我们应该都能猜测到,文件传出过大,超出了WCF默认范围,那么我们需要进行修改。

服务端和客户端都需要修改。

第一、客户端:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ITaskService"  />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:9001/wcf/TaskService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITaskService"
                contract="BZAService.ITaskService" name="BasicHttpBinding_ITaskService" />
        </client>
    </system.serviceModel>

  

上面是wcf客户端默认生成的,如需要修改传输文件大小,需要一下改动

     
<binding name="BasicHttpBinding_ITaskService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />

  

 
第二、服务端:
 
<system.serviceModel>
       <services>    
      <service name="BZACenterService.AssetService.TaskService">
        <endpoint address="" binding="basicHttpBinding" contract="BZACenterService.AssetService.ITaskService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/BZACenterService.AssetService/TaskService/" />
          </baseAddresses>
        </host>
      </service>      
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>       
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>       
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

  上面是服务端默认配置,我们发现并没有bindings,如何设置那,方法是手动在<services>上增加如下

<bindings>
     <basicHttpBinding>
        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
             transferMode="Streamed" sendTimeout="00:10:00" />
</binding>

  然后记得在在service 力的endpoint  增加属性 bindingConfiguration="LargeDataTransferServicesBinding"

最终如下:

<system.serviceModel>
 <bindings>
      <basicHttpBinding>
        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
                  transferMode="Streamed" sendTimeout="00:10:00"
                 />
      </basicHttpBinding>
    </bindings>
       <services>    
      <service name="BZACenterService.AssetService.TaskService">
        <endpoint address="" binding="basicHttpBinding" contract="BZACenterService.AssetService.ITaskService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/BZACenterService.AssetService/TaskService/" />
          </baseAddresses>
        </host>
      </service>      
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>       
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>       
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

 第三、测试运行

      最后编译运行,正常调用wcf了。

 

 

以上是关于WCF服务端返回:(413) Request Entity Too Large的主要内容,如果未能解决你的问题,请参考以下文章

远程服务器返回了意外相应:(413) Request Entity Too Large。

WCF 相关配置

WCF:远程服务器返回错误:(413)请求实体太大[重复]

具有 30mb 输入字符串的 WCF 服务 - 远程服务器返回错误:(413)请求实体太大

WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)

使用 WCF 服务传输大文件 - 检索错误 413