2

在包含该行的 finally 块中引发异常response.Close();

try
{
    response = (FtpWebResponse)request.GetResponse();
}
catch (System.Net.WebException webEx)
{
    FtpWebResponse ftpResponse = (FtpWebResponse)webEx.Response;

    if (ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
    {
        return false;
    }

    throw;
}
finally
{
    if (response != null)
    {
        response.Close();
    }
}

这是堆栈跟踪:

   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.Stream.Dispose()
   at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
   at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.CommandStream.CheckContinuePipeline()
   at System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState)
   at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   at System.Net.FtpDataStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.Net.FtpWebResponse.Close()
   at FVC.RequestForStock.Application.RequestForStockDataImportProvider.VerifyDirectoryExist(String serverAddress, String userName, SecureString password) in C:\CoName.RFS.Application\RFSDataImportProvider.cs:line 122

最后调试检查器显示的内容

在此处输入图像描述

(如果您无法辨认出文字,请放大图片,例如在 Firefox 中按住 ctrl 并向上滚动鼠标滚轮) 有人可以协助确定异常发生的原因以及如何防止异常发生。 编辑:使用 Microsoft.NET 框架的 v4.0

4

1 回答 1

0

我还会检查流是否仍然可读。

       finally
        {
            if (response != null && 
                 response.GetResponseStream()!=null && 
                 response.GetResponseStream().CanRead)
                {
                    response.Close();
                }
            }
于 2012-08-23T05:57:57.090 回答