抛出QuotaExceededException的WCF文件传输服务

时间:2016-06-19 17:10:09

标签: c# wcf

我的WCF文件传输服务在我的文件大于1.5 GB时抛出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以上的任何东西

1 个答案:

答案 0 :(得分:1)

感谢steve16351,我没有意识到maxReceivedMessageSize很长。我使用Int64.MaxValue(9223372036854775807)更新了我的配置文件,并且能够上传6.8 GB的文件。

<basicHttpBinding>
    <binding name="FileTransfer" maxReceivedMessageSize="9223372036854775807" 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 = 0L;  
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);  // no more exception  
    Logger.LogException(ex);  
}