0

I have the following problem with FtpWebResponse class in c# .Net 4.5.1 runtime. I have a FTP server with a lot of files (around 1550 files). I have a code from an older project someone else coded, it checks whether a connection to the FTP server can be achieved and the specified folder exists. The code is more or less like this:

string ftpUri= "some_ftp_url folder";
FtpWebRequest queryFTP = CreateFtpRequest(ftpUri);
queryFTP.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse webResponse = null;
try
{
    webResponse= (FtpWebResponse)queryFTP.GetResponse();        
}
catch (Exception ex)
{
    // Do some loging stuff
    return false;
}
finally
{
    webResponse?.Close();
}
return true;

The problem is than while runnning in debug mode, if I trace step by step, everything works OK. However, if I run the application without tracing, I get a 550 error when it reaches this line

    webResponse?.Close();

If I lower the amount of files in the FTP server it works OK for all the cases. I suspect it has something to do with the fact that there are a lot of files on the FTP folder and somehow, webResponse has not finished to perfom the GetResponse() method before the call to Close() is received. However, I am really puzzled by this behaviour. Any help will be appreciated.

I share the exception trace. It is an exception of type System.Net.WebException:

remote server returned an error: (550) File unavailable (eg, file not found, can not access the file)

and the exception trace:

   à System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   à System.Net.CommandStream.Dispose(Boolean disposing)
   à System.IO.Stream.Close()
   à 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.CommandStream.Abort(Exception e)
   à System.Net.CommandStream.CheckContinuePipeline()
   à System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   à System.Net.FtpDataStream.Dispose(Boolean disposing)
   à System.IO.Stream.Close()
   à System.Net.FtpWebResponse.Close()

Here is an extract of the log file. It's in french, but it is understandable.

System.Net Information: 0 : [16364] ConnectStream#41366637::ConnectStream(Mise en mémoire tampon de 6954 octets.)
System.Net Information: 0 : [16364] Associating HttpWebRequest#38166093 with ConnectStream#41366637
System.Net Information: 0 : [16364] Associating HttpWebRequest#38166093 with HttpWebResponse#39307165
System.Net Information: 0 : [16364] ContentLength=-1
System.Net Information: 0 : [16364] ContentLength=-1
System.Net Information: 0 : [16364] FtpWebRequest#37702660::.ctor(ftp://ftpserver.my.local/FtpFolder/Stored)
System.Net Information: 0 : [16364] FtpWebRequest#37702660::GetResponse(Méthode=NLST.)
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Connexion créée entre 10.15.27.102:64253 et 172.24.18.22:21.
System.Net Information: 0 : [16364] Associating FtpWebRequest#37702660 with FtpControlStream#34238661
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [220 Microsoft FTP Service]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [USER MYDOMAIN\MYUSER]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [331 Password required]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [PASS ********]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [230-Directory has 98,383,212,544 bytes of disk space available.
230 User logged in.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [OPTS utf8 on]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [200 OPTS UTF8 command successful - UTF8 encoding now ON.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [PWD]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [257 "/" is current directory.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [CWD FtpFolder]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [250 CWD command successful.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [TYPE I]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [200 Type set to I.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [PASV]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [227 Entering Passive Mode (172,24,18,22,200,148).]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Envoi de la commande [NLST Stored]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [125 Data connection already open; Transfer starting.]
System.Net Information: 0 : [16364] FtpControlStream#34238661 - Réponse reçue [550 ]
System.Net Information: 0 : [16364] FtpWebRequest#37702660::(Libération de la connexion FTP # 34238661.)
Alfredo A.
  • 1,697
  • 3
  • 30
  • 43

0 Answers0