【发布时间】:2016-10-20 22:56:49
【问题描述】:
当我的文件大于 1.5 GB 时,我有一个 WCF 文件传输服务抛出 QuotaExceededException。我查看了类似的帖子,但我不明白为什么会出现异常或如何解决它。
System.ServiceModel.QuotaExceededException:已超出传入消息的最大消息大小配额 (2147483647)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。
这是我的客户app.config的片段:
<basicHttpBinding>
<binding name="BasicHttpBinding_IFileTransfer"
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
transferMode="Streamed"
receiveTimeout="00:30:00" sendTimeout="01:30:00"
openTimeout="00:30:00" closeTimeout="00:30:00">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>
这是我的web.config 的片段:
<basicHttpBinding>
<binding name="FileTransfer" maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647" transferMode="Streamed"
receiveTimeout="00:30:00" sendTimeout="01:30:00"
openTimeout="00:30:00" closeTimeout="00:30:00">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>
以下是引发异常的服务代码片段:
var sum = 0;
try
{
FileStream targetStream;
var sourceStream = request.FileByteStream;
using (targetStream = new FileStream(tfile, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 1024 * 64;
var buffer = new byte[bufferLen];
int count;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
sum += count;
}
targetStream.Close();
sourceStream.Close();
}
}
catch (Exception ex)
{
Logger.Debug("sum = " + sum); // sum = 1610609664 bytes (this is 1.499997 GB)
Logger.LogException(ex);
}
我无法获得大于 1.5 GB 的容量
【问题讨论】:
-
maxReceivedMessageSize是传输的总数据,包括所有标头和实际传输的数据大小。例如,如果作为soap 消息传输并且数据是Base64 编码的,那么实际传输的数据将会有很多开销,因此您可能会使用1.5GB 的文件达到2GB 的限制。您需要增加限制(它将采用 Int64.MaxValue,但可能仅在绑定代码中,请参阅stackoverflow.com/questions/1004717/…)