WCF 流式传输错误(最大邮件大小配额)

Posted

技术标签:

【中文标题】WCF 流式传输错误(最大邮件大小配额)【英文标题】:WCF Streamed errror (maximum message size quota) 【发布时间】:2011-09-18 19:13:27 【问题描述】:

我有这个配置的 WCF 服务库:

<basicHttpBinding>
   <binding name="httpLargeMessageStream" 
        maxReceivedMessageSize="2147483647"      
        messageEncoding="Mtom" transferMode="Streamed" />
</basicHttpBinding>

<netTcpBinding>
    <binding name="tcpLargeMessageStream" transferMode="Streamed" 
             maxReceivedMessageSize="2147483647" />
</netTcpBinding>

如果我从客户端发送上传文件的请求,那么一切正常

public void UploadFile(FileUploadMessage request)

   try
   
      // Gets absolute local storing path 
      string localPath = Path.Combine(basePath, request.UploadMetadata.StoringPath);

      // Create folders in they don't exist
      FileInfo file = new System.IO.FileInfo(localPath);
      file.Directory.Create();

      // Get document path on server
      string serverFileName = Path.Combine(localPath, request.UploadMetadata.HashFileName);

      // Create a new temp document 
      using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
      
         // Read buffer
         const int bufferSize = 65536;

         // Output buffer 
         Byte[] buffer = new Byte[bufferSize];
         int bytesRead;

         // Write bytes from source into local file
         while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0)
         
            outfile.Write(buffer, 0, bytesRead);
         
      
   
   catch (IOException e)
   
      throw new FaultException<IOException>(e);
   

但是对于下载请求我已经收到错误:

最大邮件大小配额 传入消息 (65536) 已 超过。要增加配额,请使用 MaxReceivedMessageSize 属性 适当的绑定元素。

public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)

   try
   
      controlServerAdress = "http://localhost:8080/ControlServer/";

      EndpointAddress basicBinding = new EndpointAddress(controlServerAdress + "TokenService/basic");
      tokenService = new TokenServiceClient("BasicHttpBinding_ITokenService", basicBinding);

      // Get file token form control server
      ComDocFile file = tokenService.ResolveToken(request.DownloadMetadata.Token);

      // If exist file for token
      if (file != null)
      
         // Get document path on server
         string serverFileName = Path.Combine(basePath, file.FilePath, file.FileName);

         // Set fileName 
         request.DownloadMetadata.FileName = file.FileName;

         // Return file stream
         return new FileDownloadReturnMessage(new FileStream(serverFileName, FileMode.Open, FileAccess.Read), new ReturnDownloadMetaData(file.FileName, true));
      

      return new FileDownloadReturnMessage(null,
                    new ReturnDownloadMetaData(null, false));
   
   catch (IOException e)
   
      throw new FaultException<IOException>(e);
   

客户端调用方式:

 // Read buffer
 const int bufferSize = 65536;

 // Output buffer 
 Byte[] buffer = new Byte[bufferSize];
 int bytesRead;

 // Prepare download stream
 Stream donwloadStream;

 ReturnDownloadMetaData file = fileClient.DownloadFile(downloadMetaData, out donwloadStream);

 // If file server return valid file stream
 if (file.IsValid)
 
    // Create a new temp document 
    using (FileStream outfile = new FileStream("C:\\#ComDocs_FileServer\\" + file.FileName, FileMode.Create))
    
       // Write bytes from source into local file
       while ((bytesRead = donwloadStream.Read(buffer, 0, bufferSize)) > 0)
       
          outfile.Write(buffer, 0, bytesRead);
       
    
 

【问题讨论】:

【参考方案1】:

maxReceievedMessageSize 是接收者准备接受的数据量。在上面的代码中,对于下载,客户端是接收者。您还需要增加客户端中的 maxReceievedMessageSize (从您显示的代码中您似乎没有这样做)

【讨论】:

【参考方案2】:

使用这个

maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"

在发送者和接收者端。

【讨论】:

以上是关于WCF 流式传输错误(最大邮件大小配额)的主要内容,如果未能解决你的问题,请参考以下文章

WCF 最大消息大小配额

已超出传入邮件的最大邮件大小配额 (65536)

WCF,已超出传入邮件的最大邮件大小配额 (65536)

WCF 中的 MaxReceivedMessageSize 错误

WCF 服务 400 错误请求

我无法更改传入邮件的最大大小配额,WCF服务