Attempt to upload to FTP: System.Net.WebException: system error

I have an API that accepts XML and end up loading files based on information in XML. The download is scheduled (also from XML), and I checked everything that was around, and I know that it works.

I get an error about 40% of the time in the first file that I try to load in each time cycle (time cycle = 45 minutes for some files, 30 minutes for others).

Here is my download code:

try { LoggerFTP.Log("Uploading file: " + filename, false); // Create the request. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(appSettingsFTP.ftpUrl + @"/" + filename); request.Method = WebRequestMethods.Ftp.UploadFile; request.Timeout = 6000000; //set to 100 minutes //request.Timeout = -1; //set to infinite // Add the login credentials. request.Credentials = new NetworkCredential(appSettingsFTP.ftpLogin, appSettingsFTP.ftpPassword); // Grab the file contents. StreamReader sourceStream = new StreamReader(appSettingsFTP.uploadFileDirectory + filename); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; // Copy the file contents to the outgoing stream. Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Logger.Log(filename.ToString() + " " + "Upload Complete, Status: " + response.StatusCode + " " + response.StatusDescription, false); //Took response.StatusDescription out because it appears to be creating extra line feeds. LoggerFTP.Log(filename.ToString() + " " + "Upload Complete, Status: " + response.StatusCode, false); } catch (Exception ex) { LoggerFTP.Log(ex.ToString(), false); } 

I investigated the problem and saw that something online about this could potentially be fast-acting. For example, there is a timeout. But I have a wait time of up to 100 minutes for my FtpWebRequest, so maybe this is not the case? I dont know. It also works as a service, so it’s hard to test this aspect of the code.

Here is the exception that is being logged in my registrar (e.ToString):

 System.Net.WebException: System error. ---> System.Net.InternalException: System error. at System.Net.PooledStream.PrePush(Object expectedOwner) 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.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.AttemptedRecovery(Exception e) at System.Net.FtpWebRequest.SubmitRequest(Boolean async) --- End of inner exception stack trace --- at System.Net.FtpWebRequest.GetRequestStream() at CPMainSpringAPIExportsSC.UploadFTP.FTPUploadMethod(String viewname, String filename) 
+4
source share
2 answers

I get exactly the same stack trace in an SSIS package trying FTP over SSL. It works fine without SSL, but as soon as I enable SSL, it explodes.

  System.Net.WebException: System error. ---> System.Net.InternalException: System error. at System.Net.PooledStream.PrePush(Object expectedOwner) 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.IO.Stream.Close() at System.Net.ConnectionPool.Destroy(PooledStream pooledStream) at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) at System.Net.FtpWebRequest.AttemptedRecovery(Exception e) at System.Net.FtpWebRequest.SubmitRequest(Boolean async) --- End of inner exception stack trace --- at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream() at ST_0ff7348de65a468bb358ab0206e3721f.ScriptMain.Main() in c:\Users\Stephens\AppData\Local\Temp\Vsta\e664c8a71bb647ff9e9dc6ac32d7b615\ScriptMain.cs:line 155 at System.Net.FtpWebRequest.CheckError() at System.Net.FtpWebRequest.GetRequestStream() at ST_0ff7348de65a468bb358ab0206e3721f.ScriptMain.Main() in c:\Users\Stephens\AppData\Local\Temp\Vsta\e664c8a71bb647ff9e9dc6ac32d7b615\ScriptMain.cs:line 155 

Since the error is so general, I decided to look at the .NET source to find out if I can catch the key to the question of what is happening. If you are here:

http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.Net# {% 22pageClientState% 22% 3A% 22type-2844% 2Ccsharp% 22}

and go to line 281, you will see the definition for the inner void PrePush (object expectedOwner), which is what is executed when the exception occurs. Here's what it looks like:

  internal void PrePush(object expectedOwner) { lock (this) { //3 // The following tests are retail assertions of things we can't allow to happen. if (null == expectedOwner) { if (null != m_Owner && null != m_Owner.Target) throw new InternalException(); // new unpooled object has an owner } else { if (null == m_Owner || m_Owner.Target != expectedOwner) throw new InternalException(); // unpooled object has incorrect owner } m_PooledCount++; if (1 != m_PooledCount) throw new InternalException(); // pushing object onto stack a second time if (null != m_Owner) m_Owner.Target = null; } } 

In the end, I found that FtpWebRequest only supports explicit FTP (port 21) and not implicit FTP (port 990). This was finally stated here:

Does .NET support FtpWebRequest both implicit (FTPS) and explicit (FTPES)?

Anyway, in my case it was a firewall issue. Initially, we configured for implicit FTP, which was ports 989, 990, 49152-65535 (for each vendor technical staff). I checked with my network guy, and we opened ports 20, 21, 989, 990 and 40000-655535 for explicit, and now everything worked like a champion.

However, in your case, it seems that you are initiating a connection pool. Here is a good post on this:

How to increase the performance of FtpWebRequest?

You might want to take a look at how to configure the connection pool and see if you can make some progress. Hope this helps!

Hello,

Stewart

+4
source

I know this is an old topic, but for those with the same problem, the code is missing:

  request.EnableSsl = false; 
0
source

All Articles