好吧,我已经为我的问题寻找了几个小时的答案,但我就是无法摆脱。我正在使用 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);
}
如果你们中的一些人能给我一些意见以帮助我,我将不胜感激。谢谢!