无法从传输连接读取数据:现有连接被远程主机强制关闭
当我通过 HTTP 传输大约 50Mb 的文件时有时,我会收到此错误:
无法从传输连接读取数据:现有连接被远程主机强制关闭。
最重要的是,我在 http://stackoverflow.com 下找不到任何解决方案。
有什么线索我需要改进/改变吗?
应用程序配置:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MyDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
<binding name="BasicHttpBinding_MyAuthenticationDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
代码:
private void FinishWebRequest(IAsyncResult result)
{
// Assign values to these objects here so that they can be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
try
{
response = request.EndGetResponse(result);
if (response != null)
{
// Once the WebResponse object has been retrieved, get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
string pathToSaveFile = Path.Combine(FileManager.GetFolderContent(), FileView.Filename);
localStream = File.Create(pathToSaveFile);
WinAPI.SYSTEM_INFO sysinfo = new WinAPI.SYSTEM_INFO();
WinAPI.GetSystemInfo(ref sysinfo);
// Allocate a buffer
byte[] buffer = new byte[int.Parse(sysinfo.dwPageSize.ToString())];
int bytesRead;
// Simple do/while loop to read from stream until no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
BytesProcessed += bytesRead;
} while (bytesRead > 0);
FileView.Downloaded = DateTime.Now;
if (BytesProcessed > 0)
{
FileView.IsSuccess = true;
FileView.IsDownloading = false;
}
else
{
FileView.IsSuccess = false;
FileView.IsDownloading = false;
}
}
}
catch (Exception ex)
{
#region Error
LogEntry l = new LogEntry();
l.Message = string.Format("{0}", ex.Message);
l.Title = "FinishWebRequest() Error";
l.Categories.Add(Category.General);
l.Priority = Priority.Highest;
if (ex.InnerException != null) l.ExtendedProperties.Add("InnerException", ex.InnerException.Message);
CustomLogger.CustomLogger.WriteErrorLog(l);
#endregion
}
finally
{
// Close the response and streams objects here to make sure they're closed even if an exception is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
}
When I transfer a file about 50Mb over HTTP sometimes I get this error:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
First of all I couldn't find any solution under http://stackoverflow.com.
Any clue what I have to improve/change?
app.config:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_MyDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
<binding name="BasicHttpBinding_MyAuthenticationDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
code:
private void FinishWebRequest(IAsyncResult result)
{
// Assign values to these objects here so that they can be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
try
{
response = request.EndGetResponse(result);
if (response != null)
{
// Once the WebResponse object has been retrieved, get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
string pathToSaveFile = Path.Combine(FileManager.GetFolderContent(), FileView.Filename);
localStream = File.Create(pathToSaveFile);
WinAPI.SYSTEM_INFO sysinfo = new WinAPI.SYSTEM_INFO();
WinAPI.GetSystemInfo(ref sysinfo);
// Allocate a buffer
byte[] buffer = new byte[int.Parse(sysinfo.dwPageSize.ToString())];
int bytesRead;
// Simple do/while loop to read from stream until no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
BytesProcessed += bytesRead;
} while (bytesRead > 0);
FileView.Downloaded = DateTime.Now;
if (BytesProcessed > 0)
{
FileView.IsSuccess = true;
FileView.IsDownloading = false;
}
else
{
FileView.IsSuccess = false;
FileView.IsDownloading = false;
}
}
}
catch (Exception ex)
{
#region Error
LogEntry l = new LogEntry();
l.Message = string.Format("{0}", ex.Message);
l.Title = "FinishWebRequest() Error";
l.Categories.Add(Category.General);
l.Priority = Priority.Highest;
if (ex.InnerException != null) l.ExtendedProperties.Add("InnerException", ex.InnerException.Message);
CustomLogger.CustomLogger.WriteErrorLog(l);
#endregion
}
finally
{
// Close the response and streams objects here to make sure they're closed even if an exception is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是所有情况的解决方案。
但是,当我将下载大文件的服务器并行连接数量从 3 个减少到 2 个时,就没有错误了。
(早期我使用3个并联连接。)
I am not sure if it is a solution for all cases.
But when I reduced the number of parallel connections to the server to download big files from 3 to 2 only there are no error.
(Early I used 3 parallel connections.)
我遇到了类似的问题,并通过在 web.config 中添加行为部分来解决该问题,该部分告诉数据协定序列化程序覆盖默认的 64kb 大小限制。
另外,添加配置后,我开始在客户端收到相同问题的错误,您需要将此配置添加到服务和客户端。
I ran into a similar issue and was able to resolve it by adding a behaviors section to my web.config which tells the data contract serializer to override the default 64kb size limit.
Also, after adding in the config I started getting an error on the client side that was the same issue, you’ll need to add this config to both your service and client side.