ReadAsync抛出IOException

时间:2014-12-17 22:19:03

标签: c# asynchronous

我有一个可以抓取数千个网页的Windows服务,我注意到ReadAsync方法间歇性地抛出IOExceptions,我已经确认这是由于TCP窗口大小达到0而客户端无法访问处理更多数据。是否可以为该方法增加TCP窗口大小缓冲区?您还建议采用什么方法来克服IOException?可以公平地说,ReadAsync方法在处理多个请求时是不好的 - 因为这是抛出异常的时间,它可以处理一些下载,但不过说32它会间歇性地抛出异常。我已经确认它不是带宽,内存或cpu问题,因为我监控这些资源并且它们似乎没有被过度使用。

实际例外是:

IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.PooledStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) 
at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) 
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state, TaskCreationOptions creationOptions) at System.Threading.Tasks.TaskFactory`1.FromAsync[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state) 
at System.IO.StreamExtensions.ReadAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count) in C:\app\Extras\C#,C++,F#,VB\Extras\Extensions\APM\StreamExtensions.cs:line 29 
at App.StreamExtensions.<CopyStreamIterator>d__6.MoveNext() in c:\app\Extensions\StreamExtensions.cs:line 199 
at System.Threading.Tasks.TaskFactoryExtensions.<>c__DisplayClassa.<Iterate>b__7(Task antecedent) in c:\app\Extras\C#,C++,F#,VB\Extras\Extensions\TaskFactoryExtensions\TaskFactoryExtensions_Iterate.cs:line 183

我已经确认,由于TCP窗口大小使用wireshark达到0,我可以看到服务器发送连接重置。我还查看了客户端发送的最后一个请求,该请求显示tcp窗口大小为零。基本上我试图将流复制到内存,有趣的是如果我使用非异步CopyTo方法,我得到的缓冲区溢出没有数百个请求。请记住,每个请求都在一个单独的线程中运行,因此请求本身仍然是并行的。

0 个答案:

没有答案