none
FtpWebRespose Object can't close when the files count over 7000 RRS feed

  • Question

  • I'm check the remote folder whether exists, but if the files count is over 7000 in ftp server, I will got ftp error 550 when closing the FtpWebResponse object.

    here is my code

    public bool IsExistsRemoteDirectory(string remotePath)
            {
               
                if (remotePath == null)
                {
                    throw new ArgumentNullException();
                }
    
                
                
                if (!remotePath.EndsWith("/"))
                {
                    remotePath += "/";
                }
    
                FtpWebResponse response = null;
                try
                {
                    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(CeraeUri(remotePath));
    
                    ftpRequest.Credentials = new NetworkCredential(ftpOption.UserId, ftpOption.Password);
                    ftpRequest.UseBinary = true;
                    ftpRequest.KeepAlive = false;
                    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    
                    
                    response = (FtpWebResponse)ftpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    
                    FtpWebResponse ftpResponse = (FtpWebResponse)ex.Response;
                    if (ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    {
                        return false;
                    }
    
                    
                    throw HandleFtpException(ex, remotePath, FtpOperationKind.Get);
                }
                catch (Exception ex)
                {
                    
                    throw HandleFtpException(ex, remotePath, FtpOperationKind.Get);
                }
                finally
                {
                    
                    if (response != null)
                    {
                        response.Close();
                    }
                }
    
                return true;
            }

    and the error stack is as following.

    System.Net.WebException
         場所 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
         場所 System.Net.FtpWebRequest.RequestCallback(Object obj)
         場所 System.Net.CommandStream.Dispose(Boolean disposing)
         場所 System.IO.Stream.Close()
         場所 System.IO.Stream.Dispose()
         場所 System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
         場所 System.Net.ConnectionPool.PutConnection(PooledStream pooledStream,   Object owningObject, Int32 creationTimeout, Boolean canReuse)
         場所 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
         場所 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
         場所 System.Net.FtpWebRequest.RequestCallback(Object obj)
         場所 System.Net.CommandStream.Abort(Exception e)
         場所 System.Net.CommandStream.CheckContinuePipeline()
         場所 System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState)
         場所 System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState   closeState)
         場所 System.Net.FtpDataStream.Dispose(Boolean disposing)

    I have known how to solve it , but I don't know the reason.

    Method 1:

    after ftpRequest.GetResponse() let the thread sleep 1 seconds.

    Method 2:

    after ftpRequest.GetResponse() output the ResponseStream to a object

    Can anyone tell me the reason?


    xiaohe.feng


    • Edited by fengxiaohe Wednesday, October 23, 2013 1:30 PM
    Tuesday, October 22, 2013 4:48 PM

All replies

  • Hi,

    Because when the files count over 7000, then it will be need more time to read, so in this way I would check if the stream is still readable.
           finally
            {
                if (response != null && 
                     response.GetResponseStream()!=null && 
                     response.GetResponseStream().CanRead)
                    {
                        response.Close();
                    }
                }
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 23, 2013 2:52 AM
    Moderator
  • But if there will be many response object that can not be closed, it is not good for the system, I think.

    xiaohe.feng

    Wednesday, October 23, 2013 1:32 PM