【发布时间】:2019-01-19 07:29:03
【问题描述】:
我想使用WCF 在客户端和服务器之间传输文件。我读了很多文章,有些人为此目的使用Stream 模式。但由于它的限制,我决定使用缓冲模式创建服务。例如考虑一下:
[OperationContract]
void UploadFile(Guid systemKey, string fileName, byte[] fileContent, string userName);
服务器网络配置:
...
<system.web>
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.6" maxRequestLength="2147483647" executionTimeout="7200"/>
</system.web>
....
<bindings>
<basicHttpBinding>
<binding name="myBasicBinding" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
closeTimeout="01:50:00"
openTimeout="01:50:00"
sendTimeout="01:50:00"
receiveTimeout="01:50:00"
messageEncoding="Mtom">
<readerQuotas maxDepth="128" maxStringContentLength="8388608" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
....
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295"/>
</requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
和客户端配置:
<system.web>
<compilation debug="true" targetFramework="4.6"/>
<httpRuntime targetFramework="4.6" maxRequestLength="2147483647" executionTimeout="7200"/>
</system.web>
...
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295"/>
</requestFiltering>
</security>
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicEndpoint"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
closeTimeout="01:50:00"
openTimeout="01:50:00"
sendTimeout="01:50:00"
receiveTimeout="01:50:00"
messageEncoding="Mtom">
<readerQuotas maxDepth="128" maxStringContentLength="8388608" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/fts.svc" binding="basicHttpBinding" bindingConfiguration="basicEndpoint" contract="ServiceReference1.Ifts" name="basicEndpoint"/>
</client>
</system.serviceModel>
我可以上传400MB 文件,但是当文件大小为500MB 或以上时,我收到此错误:
System.InsufficientMemoryException H结果=0x8013153D 消息=未能分配 1073741824 字节的托管内存缓冲区。可用内存量可能很低。 来源=mscorlib
内部异常 1: OutOfMemoryException:引发了“System.OutOfMemoryException”类型的异常。 \
我想我为2GB 数据设置了值,但它在500MB 上失败了。
谢谢
这段代码在本地运行,我有16GB RAM。问题出在哪里,我应该更改什么值来上传硬文件?
谢谢
【问题讨论】:
-
我认为问题出在
BufferManager,大对象实际上是由WCF分配的,从未被释放,所以导致内存泄漏异常,最好使用streamed模式。
标签: c# wcf wcf-binding c#-7.0 .net-4.6