好吧,我已经找了几个小时我的问题的答案,但我不能摆脱。我正在使用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);
}
如果你们中的一些人能给我一些评论来帮助我,我将不胜感激。谢谢!
答案 0 :(得分:1)
您检查过客户端配置文件了吗?即使您修改了服务配置文件,客户端配置也始终使用默认设置生成。 请参阅我的以下博文,其中详细介绍了如何解决此错误。
答案 1 :(得分:0)
更改此
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
答案 2 :(得分:0)
毕竟,蒂姆在我的问题中评论了这件事。我使用自定义类来创建我的绑定(ClientManager.CreateCAVServiceClient)我没有更改该类中的绑定,因此缓冲区大小没有被修改。谢谢Tim解决我的问题,其他人给我建议:D!