0

好吧,我已经为我的问题寻找了几个小时的答案,但我就是无法摆脱。我正在使用 WCF Web 服务从服务器下载文件。为了测试,我使用了 3 个不同的文档 (PDF),其中 1 个大小为 24Kb,另外 2 个大小为 60KB。我可以下载第一个(24KB),但其他都不能下载,每次我尝试时,Visual Studio 都会显示此异常:

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

我读过缓冲区的默认大小是 64KB,所以我不知道为什么我不能下载 60KB 大小的文件。我尝试更改我的配置(服务器和客户端)中的缓冲区大小,甚至尝试使用流传输及其相同的东西。

这是我的 .config 文件代码(在服务器上):

<system.serviceModel>

<services>      
  <service behaviorConfiguration="MyServiceTypeBehaviors" name="CAVTransactions">
    <endpoint address="CAV" binding="basicHttpBinding" bindingConfiguration="bindingIntegrator"
      name="CAV" contract="ICAVContract" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://XX.XXX.X.XX:XXXX/App_Code/" />
      </baseAddresses>
    </host>
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="bindingIntegrator"
             maxReceivedMessageSize="2147483647"
             maxBufferSize="2147483647" transferMode="Streamed" >

      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
           maxArrayLength="2147483647" maxBytesPerRead="2147483647"
           maxNameTableCharCount="2147483647"/>
    </binding>
  </basicHttpBinding>
</bindings>

<behaviors>

  <serviceBehaviors>

    <behavior name="MyServiceTypeBehaviors">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="6553600"/>
    </behavior>

  </serviceBehaviors>
</behaviors>    

我的 .config 文件代码(在客户端):

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
          <binding name="bindingIntegrator"
           maxReceivedMessageSize="2147483647"
           maxBufferSize="2147483647" transferMode="Streamed" >

            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                 maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                 maxNameTableCharCount="2147483647"/>
          </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/IntegratorWCFService/CAVTransactions.svc/CAV"
            binding="basicHttpBinding" bindingConfiguration="bindingIntegrator" contract="CAVService.ICAVContract"
            name="CAV" />
    </client>
</system.serviceModel>

我的合同:

[OperationContract]
Stream DownloadFileOther(string documentName);

public Stream DownloadFileOther(string documentName)
{

    System.IO.Stream str;
    try
    {
        //string filePath = System.IO.Path.Combine(@"C:\CAV_Documents", request.FileName);
        string filePath = System.IO.Path.Combine(@"C:\CAV_Documents", documentName + ".pdf");
        FileInfo fileInfo = new FileInfo(filePath);

        //Chequeo de existencia
        if (!fileInfo.Exists)
        {
            throw new FileNotFoundException("Archivo no encontrado", documentName);
        }

        //Apertura de stream
        FileStream stram = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        str = stram;

        //resultado
        //result.FileName = request.FileName;
        //result.Length = fileInfo.Length;
        //result.FileByteStream = stram;

    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        throw new NotSupportedException();
    }

    return str;
    //return 0;
}

以及下载文件的代码:

try
                {
                    Stream stream;

                    string codigo = selectedRevision.Code.ToString();

                    stream = ClientManager.CreateCAVServiceClient().DownloadFileOther(codigo);

                    string key = Guid.NewGuid().ToString();


                    using (var file = File.Create("Temp\\" + key + ".pdf"))
                    {
                        stream.CopyTo(file);
                    }

                    string GuidePath = @"./Temp/" + key + ".pdf";
                    string fullPath = System.IO.Path.GetFullPath(GuidePath);
                    Uri GuideURI = new Uri(fullPath, UriKind.Absolute);

                    selectedRevision.DocumentPath = GuideURI;

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

如果你们中的一些人能给我一些意见以帮助我,我将不胜感激。谢谢!

4

3 回答 3

1

你检查你的客户端配置文件了吗?即使您修改了服务配置文件,客户端配置也始终使用默认设置生成。请参阅我的以下博客文章,其中详细介绍了如何解决此错误。

\ http://thetechnocrate.wordpress.com/2012/05/24/the-maximum-message-size-quota-for-incoming-messages-65536-has-been-exceeded/

于 2013-05-01T05:02:00.237 回答
0

毕竟,这是蒂姆在我的问题中评论的。我正在使用自定义类来创建我的绑定(ClientManager.CreateCAVServiceClient)我没有更改该类中的绑定,因此缓冲区大小没有被修改。感谢 Tim 解决了我的问题,以及其他人给了我建议:D!

于 2013-05-02T13:48:20.123 回答
0

在您的服务器配置中更改此

<serviceBehaviors>

<behavior name="MyServiceTypeBehaviors">
  <serviceMetadata httpGetEnabled="true" />
  <serviceDebug includeExceptionDetailInFaults="true" />
  <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>

于 2013-05-01T12:53:05.203 回答