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

时间:2011-06-20 15:16:53

标签: wcf

我有这个配置的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);
       }
    }
 }

2 个答案:

答案 0 :(得分:4)

maxReceievedMessageSize是接收者准备接受的数据量。在上面的代码中,为了下载,客户端是接收者。您还需要在客户端中增加maxReceievedMessageSize(您似乎没有从您显示的代码中执行此操作)

答案 1 :(得分:0)

使用此

maxBufferSize =“2147483647”maxReceivedMessageSize =“2147483647”

发送方和接收方都是