【问题标题】:WCF error: The remote server returned an error: (413) Request Entity Too LargeWCF 错误:远程服务器返回错误:(413)请求实体太大
【发布时间】:2019-06-25 12:54:19
【问题描述】:

我创建了一个 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 上更正

【问题讨论】:

  • 你检查过IIS中的uploadReadAheadSize
  • 是已经配置为uploadReadAheadSize = 8388608
  • 我遇到了这个问题,通过更改 Nginx 中的值解决了这个问题。不确定您是否正在使用它,但这就是解决我的问题的原因。
  • 感谢 Usman,但我没有使用 Nginx。任何其他解决方案。

标签: c# wcf iis-8 wcf-binding


【解决方案1】:

请参考下面的配置,它同时支持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
如果问题仍然存在,请随时告诉我。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 2021-10-08
    • 2014-07-23
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多