使用字节数组传输大文件的问题 [英] Problem with Transfer large File using byte array
问题描述
大家好,
我在WCF服务中使用字节数组传输来传输大文件时遇到问题.下面是代码,我正在使用
I am facing a issue with transferring large file using byte array transfer in WCF Service. Below is the code, I am using
使用(FileStream流= File.OpenRead(files [i]))
nbsp; bsp //使用(FileStream writeStream
= File.OpenWrite(RunningPath + @" \ TempFiles \" + FileName))
nbsp; bsp {
nbsp; bsp
BinaryReader reader =新的BinaryReader(stream);
nbsp; bsp
//BinaryWriter writer = new BinaryWriter(writeStream);
nbsp; bsp
byte []缓冲区=新的Byte [maxBufferSize];
nbsp; bsp
int bytesRead;
nbsp; bsp
而((bytesRead =
nbsp; bsp nbsp; bsp
stream.Read(buffer,0,maxBufferSize))> 0)
nbsp; bsp
{
nbsp; bsp
//writeStream.Write(buffer,0,bytesRead);
nbsp; bsp nbsp; b
= objScan.TransferFileByChunks(buffer,buffer.Length,fileName);
nbsp; bsp
nbsp; bsp
}
nbsp; bsp
Array.Clear(buffer,0,maxBufferSize);
nbsp; bsp
缓冲区= null;
nbsp; bsp
stream.Close();
nbsp; bsp
nbsp; bsp }
using (FileStream stream = File.OpenRead(files[i]))
//using (FileStream writeStream
= File.OpenWrite(RunningPath + @"\TempFiles\" + FileName))
{
BinaryReader reader = new BinaryReader(stream);
//BinaryWriter writer = new BinaryWriter(writeStream);
byte[] buffer = new Byte[maxBufferSize];
int bytesRead;
while ((bytesRead =
stream.Read(buffer, 0, maxBufferSize)) > 0)
{
//writeStream.Write(buffer, 0, bytesRead);
successfull
= objScan.TransferFileByChunks(buffer, buffer.Length, fileName);
}
Array.Clear(buffer, 0, maxBufferSize);
buffer = null;
stream.Close();
}
objScan是我的服务对象.
objScan is my service Object.
我观察到一些事情,当我设置maxBufferSize = 100 KB时,我可以调试我的了.服务代码,但是当我将maxBufferSize设置为100 MB时,出现以下异常
I observed few things, when I set maxBufferSize = 100 KB I am able to debug my service code but when I set the maxBufferSize = 100 MB, I get the below exception
"服务器未提供有意义的回复;这可能是由于合同不匹配,会话过早关闭或内部服务器错误引起的."
"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."
尽管我已将WCF服务配置中的所有最大大小都设置为其最大值(2147483647).
Although I have set all max size in WCF Service config to its max value (2147483647).
我正在使用wsDualHttpBinding.以下是服务配置的一部分.
I am using wsDualHttpBinding. Below is part of Service configuration .
< system.serviceModel>
< serviceHostingEnvironment multipleSiteBindingsEnabled ="true"; />
<服务>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<服务行为配置="DMS.DataAccess.ServiceBehavior"; name =" DMS.DataAccess.ScanDataAccess">
<端点地址=" binding ="wsDualHttpBinding" bindingConfiguration ="DMS.DataAccess.WSBindingBehavior.Scan". contract ="DMS.DataAccess.IScanContract" />
<端点地址="mex"; binding ="mexHttpBinding" contract ="IMetadataExchange" />
</service>
<service behaviorConfiguration="DMS.DataAccess.ServiceBehavior" name="DMS.DataAccess.ScanDataAccess">
<endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="DMS.DataAccess.WSBindingBehavior.Scan" contract="DMS.DataAccess.IScanContract" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</services>
< wsDualHttpBinding>
<绑定名称="DMS.DataAccess.WSBindingBehavior.Scan" maxBufferPoolSize ="2147483647"; maxReceivedMessageSize ="2147483647"; closeTimeout ="03:05:00"; openTimeout =" 00:05:00" receiveTimeout ="23:10:00" sendTimeout ="03:00:00";
>
<安全模式=无">
< message clientCredentialType ="None" />
</security>
< readerQuotas maxDepth =" 2147483647" maxStringContentLength ="2147483647"; maxArrayLength ="2147483647"; maxBytesPerRead ="2147483647"; maxNameTableCharCount ="2147483647"; />
</binding>
</wsDualHttpBinding>
<wsDualHttpBinding>
<binding name="DMS.DataAccess.WSBindingBehavior.Scan" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="03:05:00" openTimeout="00:05:00" receiveTimeout="23:10:00" sendTimeout="03:00:00"
>
<security mode="None">
<message clientCredentialType="None" />
</security>
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</wsDualHttpBinding>
<行为>
< endpointBehaviors>
<行为名称="DMS.DataAccess.ServiceBehavior">
< callbackTimeouts transactionTimeout ="00:30:00"/>
< enableWebScript/>
</行为>
<行为名称="ServiceAspNetAjaxBehavior">
< enableWebScript/>
</行为>
</endpointBehaviors>
<behaviors>
<endpointBehaviors>
<behavior name="DMS.DataAccess.ServiceBehavior">
<callbackTimeouts transactionTimeout="00:30:00"/>
<enableWebScript />
</behavior>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
< serviceBehaviors>
<serviceBehaviors>
<行为名称="DMS.DataAccess.ServiceBehavior">
< serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions ="100". />
< serviceMetadata httpGetEnabled ="true"; />
< serviceDebug includeExceptionDetailInFaults ="true"; />
< dataContractSerializer maxItemsInObjectGraph ="10000000"; />
</behavior>
<behavior name="DMS.DataAccess.ServiceBehavior">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="10000000" />
</behavior>
</serviceBehaviors>
</行为>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</system.serviceModel>
请帮助我我在这里做错了..
Please help me what I am doing wrong here..
先谢谢大家.
推荐答案
您好,如果使用IIS,则可能需要设置httpRuntime请求长度:
Hi, if using IIS, you may need to set the httpRuntime request length:
<system.web> <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/> <httpRuntime maxRequestLength="100000"/> </system.web>
我们遇到了类似的问题.在IIS上设置maxRequestLength也解决了我们的问题.
We had a similar problem. Setting the maxRequestLength on IIS also solved our problem.
这篇关于使用字节数组传输大文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!