首页 > 解决方案 > WCF 错误:远程服务器返回错误:(413)请求实体太大

问题描述

我创建了一个 WCF 服务来将 1MB 的文件数据上传到 Windows Server。此 Web 服务在处理小文件 (950 KB) 数据时运行良好,但文件较大时存在问题。

我尝试了几种方法在 WCF web.config 中修复它,但仍然陷入上述错误。我需要帮助设置 WCF 配置来解决我的问题。

这是我的 web.config 如下:

<system.serviceModel>
    <services>
      <service name="WCFService.UploadService">
        <endpoint address="REST" behaviorConfiguration="WCFServiceBehavior" binding="webHttpBinding" bindingConfiguration="WCFServiceBinding" contract="WCFService.IUploadService" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="WCFServiceBinding" maxReceivedMessageSize="1073741824" maxBufferSize="20971520" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transferMode="Streamed">
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WCFServiceBehavior">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>

请在 WCF web.config 上方更正

标签: c#wcfiis-8wcf-binding

解决方案


请参考下面的配置,它同时支持 Http 和 Https。你可以根据需要剪掉它。此外,我使用 ProtocolMapping 功能来简化我的配置。

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" name="httpbinding">
          <security mode="None">
          </security>
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
        <binding name="httpsbinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <!--http and https are all supported.-->
      <add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding"/>
      <add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

对于如何简化配置,
https:
//docs.microsoft.com/en-us/dotnet/framework/wcf/simplified-configuration 如果问题仍然存在,请随时告诉我。


推荐阅读