WCF流式传输大文件时出现内存不足错误

3

我有一个用于传输文件的WCF服务。我使用基本的basicHttpBinding流模式。我在web.config和app.config(客户端)中正确配置了一些值(我猜是正确的),但它没有按照我的期望工作。它可以发送和接收高达1,610,611,200字节,大约1.5 GB。每当我上传大于此大小的文件到服务器时,我的服务方法就会抛出“读取流时引发异常”的异常。当我尝试下载大于此大小的文件时,它会抛出“System.OutOfMemoryException”类型的异常。下面是与我的配置文件相关的部分。希望有人能给我一些指点来解决这个问题。

 <basicHttpBinding> (web config)
    <binding name="StreamServiceHttpBinding" receiveTimeout="01:00:10" sendTimeout="03:00:30" maxBufferPoolSize="2147483647"
      maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      transferMode="Streamed">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>

 <basicHttpBinding> (app config)
    <binding name="BasicHttpBinding_IStreamService" receiveTimeout="01:00:10"
      sendTimeout="03:00:30" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647" transferMode="Streamed">
      <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding> 
  </basicHttpBinding>

顺便说一下,我的服务器和客户端都有8GB的内存。由于它是流传输模式,不一定使用内存,但现在我在考虑是否存在内存问题:(任何帮助将不胜感激。


你应该将其分成较小的块进行流式传输。 - jerjer
3
这是一篇有关此事的好文章:http://msdn.microsoft.com/en-us/library/ms733742.aspx。 - jerjer
很不幸,我在这篇文章中没有找到任何对我的情况有益的信息 :/ 不过还是非常感谢,我通过阅读这篇文章学到了很多好东西 ;) - Tolga Evcimen
您可以考虑按照以下方式增加httpRuntime maxrequest长度:<httpRuntime maxRequestLength="2097150"/> - Rajesh
我遇到了同样的问题。你最终做了什么? - Kyle Johnson
不幸的是,我没有想出任何简洁的解决方案。但既然我无法绕过这个问题,我决定去面对它 :) 我现在将大流分成1GB的块,然后在服务器端将它们组合起来。 - Tolga Evcimen
2个回答

0

上周我也遇到了同样的问题。我想我找到了一个解决方案。我将 maxReceivedMessageSize="4294967295"(接近4GB)进行了更改,并增加了超时时间。

app.config

<bindings>
      <basicHttpBinding>
        <binding name="GShare.Sharer" receiveTimeout="00:40:00" sendTimeout="00:40:00" maxReceivedMessageSize="4294967295" maxBufferSize="2147483647" maxBufferPoolSize="4294967295" transferMode="StreamedRequest">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None">
          </security>
        </binding>
      </basicHttpBinding>
</bindings>

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295"/>
      </requestFiltering>
    </security>
  </system.webServer>

客户端 web.config

<binding name="BasicHttpBinding_ISharer" closeTimeout="24:01:00" openTimeout="24:01:00" receiveTimeout="24:10:00" sendTimeout="24:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="4294967295" maxBufferSize="2147483647" maxReceivedMessageSize="4294967295" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true" messageEncoding="Text">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
 </binding>

之后,我成功上传了1.89GB的文件。


你是遇到了 System.OutOfMemoryException 异常还是遇到了 maxReceivedMessageSize exceeded 异常? - Tolga Evcimen
有趣的事情是,直到超时结束时我才收到错误提示,当客户端看起来只是正常页面时发生了这种情况 - 没有响应,它只是停止加载。当我查看我的服务器时,WCF上传了1.5GB,但没有更多。它就像停止线程或其他东西一样停止运行代码。 - Ali Hidim

0

感谢 @JJ_CoderHir。

我遇到了同样的问题。使用下面的代码解决了这个问题。据我理解,由于“requestLengthDiskThreshold”的默认值,我无法正确处理System.OutOfMemory问题。所以我现在已经更改了它,现在它按照我的期望工作。

<system.web>
    <httpRuntime maxRequestLength="2147483647" requestLengthDiskThreshold="2097151" executionTimeout="240"/>
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
    </security>
</system.webServer>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接