【问题标题】:Large Files in WCF ServiceWCF 服务中的大文件
【发布时间】:2016-01-29 16:00:24
【问题描述】:

我有一个 WCF SOAP 服务,我正在尝试向该服务提交一个太大 (128KB) 的文件。我在网上搜索了很多关于添加 maxReceivedMessageSize、maxBufferSize 和其他此类属性的建议,但无济于事。我还将 maxRequestLength 添加到 httpRuntime ,但这也不起作用。不管我做什么,我都会得到 HTTP 错误 413:请求实体太大。

错误

HTTP Error 413: Request Entity Too Large.

网页配置

<httpRuntime targetFramework="4.5.1" maxRequestLength="1048576"/>

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SomeBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" openTimeout="00:20:00"
          receiveTimeout="00:20:00" closeTimeout="00:20:00" sendTimeout="00:20:00">
          <readerQuotas maxDepth="200" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>
 <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

【问题讨论】:

  • WCF/SOAP 不是 HTTP 或 FTP。它们不是用来传输文件的。与其寻找增加允许文件大小的方法,不如考虑用允许 POST 文件的 HTTP 端点替换此服务。
  • 抱歉,我的意思是,我正在读取文件的内容,然后在服务中将其作为字符串发送到方法中。文件的读取超出范围并且工作正常。
  • 我明白你的要求。我是说将大文件作为 Web 服务参数发送是错误的。 如何发送它并不重要,尽管将它作为字符串参数发送比作为stream 发送它“错误”

标签: wcf .net-4.5.2


【解决方案1】:

即使您在绑定中设置了更大的配额,您的服务也不会使用该绑定,因为它从未分配到端点或设置为basicHttpBinding 的默认绑定。

由于您没有在配置文件中定义明确的端点,您可以通过省略name 属性将绑定配置设置为basicHttpBinding 的默认值,如下所示:

<binding maxBufferPoolSize="2147483647" 
         maxReceivedMessageSize="2147483647" 
         maxBufferSize="2147483647" 
         openTimeout="00:20:00"
         receiveTimeout="00:20:00" 
         closeTimeout="00:20:00" 
         sendTimeout="00:20:00">
  <readerQuotas maxDepth="200" 
                maxStringContentLength="8388608" 
                maxArrayLength="16384" 
                maxBytesPerRead="2147483647" 
                maxNameTableCharCount="16384" />
</binding>

如果不将其设为所有使用 basicHttpBinding 的请求的默认值(或将其分配给显式端点),服务将使用默认值(和更小的值)创建绑定。

有关默认绑定(以及端点和行为)的更多信息,请参阅A Developer's Introduction to Windows Communication Foundation 4

【讨论】:

    【解决方案2】:

    @Tim 我在网上的几个地方都看到了这个,它是正确的,除了你错过了其他东西。我很困惑,因为我是 WCF 的新手,我的服务包装了另一个服务,这样我就可以在不更改现有系统的情况下执行新的逻辑。因此,我所做的任何更改都只会应用于我正在调用的客户。您需要将以下&lt;services&gt; 添加到&lt;system.serviceModel&gt;,以便将这些更改应用于服务。

    <services>
          <service name="ANX.DEX001.WebService.EtPrintService">
            <endpoint name="basicHttpBinding"
                      address="/EtPrintService.svc"
                      binding="basicHttpBinding"
                      contract="ANX.DEX001.WebService.IEtPrintService"
                      bindingConfiguration="APIPortBinding"/>
          </service>
    </services>
    

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多