c# - Attempting to upload to FTP: System.Net.WebException: System error -


i have api takes in xml , uploads files based on information in xml. uploads on schedule (also xml), , have tested surrounding , know works.

i getting error 40% of time on first file attempt upload in each time cycle (time cycle = 45 minutes files, 30 minutes others).

here code upload:

try {     loggerftp.log("uploading file: " + filename, false);      // create request.     ftpwebrequest request = (ftpwebrequest)webrequest.create(appsettingsftp.ftpurl + @"/" + filename);     request.method = webrequestmethods.ftp.uploadfile;     request.timeout = 6000000; //set 100 minutes     //request.timeout = -1; //set infinite      // add login credentials.     request.credentials = new networkcredential(appsettingsftp.ftplogin, appsettingsftp.ftppassword);      // grab file contents.     streamreader sourcestream = new streamreader(appsettingsftp.uploadfiledirectory + filename);     byte[] filecontents = encoding.utf8.getbytes(sourcestream.readtoend());     sourcestream.close();     request.contentlength = filecontents.length;      // copy file contents 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 appears creating line feeds.     loggerftp.log(filename.tostring() + " " + "upload complete, status: " + response.statuscode, false); } catch (exception ex) {     loggerftp.log(ex.tostring(), false); } 

i have researched issue , saw online potentially being speed thing. like, there timeout. have timeout set 100 minutes ftpwebrequest, can't possibly that? don't know. running service hard test aspect of code.

here exception getting logged in logger (e.tostring):

system.net.webexception: system error. ---> system.net.internalexception: system error.    @ system.net.pooledstream.prepush(object expectedowner)    @ 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.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.attemptedrecovery(exception e)    @ system.net.ftpwebrequest.submitrequest(boolean async)    --- end of inner exception stack trace ---    @ system.net.ftpwebrequest.getrequeststream()    @ cpmainspringapiexportssc.uploadftp.ftpuploadmethod(string viewname, string filename) 

i getting same stack trace in ssis package attempting ftp on ssl. works great without ssl, enable ssl, blows up.

    system.net.webexception: system error. --->     system.net.internalexception: system error.    @     system.net.pooledstream.prepush(object expectedowner)    @     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.io.stream.close()    @     system.net.connectionpool.destroy(pooledstream pooledstream)    @     system.net.connectionpool.putconnection(pooledstream pooledstream, object owningobject, int32 creationtimeout, boolean canreuse)    @     system.net.ftpwebrequest.attemptedrecovery(exception e)    @     system.net.ftpwebrequest.submitrequest(boolean async)     --- end of inner exception stack trace ---    @     system.net.ftpwebrequest.checkerror()    @     system.net.ftpwebrequest.getrequeststream()    @     st_0ff7348de65a468bb358ab0206e3721f.scriptmain.main() in c:\users\stephens\appdata\local\temp\vsta\e664c8a71bb647ff9e9dc6ac32d7b615\scriptmain.cs:line 155 @      system.net.ftpwebrequest.checkerror()    @     system.net.ftpwebrequest.getrequeststream()    @     st_0ff7348de65a468bb358ab0206e3721f.scriptmain.main() in c:\users\stephens\appdata\local\temp\vsta\e664c8a71bb647ff9e9dc6ac32d7b615\scriptmain.cs:line 155 

because error generic, decided @ .net source see if can catch clue breaking. if go here:

http://labs.developerfusion.co.uk/sourceviewer/browse.aspx?assembly=sscli&namespace=system.net#{%22pageclientstate%22%3a%22type-2844%2ccsharp%22}

and skip down line 281, see definition internal void prepush(object expectedowner) executing when exception happens. here looks like:

    internal void prepush(object expectedowner)     {         lock (this) {             //3 // following tests retail assertions of things can't allow happen.             if (null == expectedowner) {                 if (null != m_owner && null != m_owner.target)                     throw new internalexception();                 // new unpooled object has 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 second time             if (null != m_owner)                 m_owner.target = null;         }     } 

eventually discovered ftpwebrequest supports explicit ftp (port 21) , not implicit ftp (port 990). definitively stated here:

does .net ftpwebrequest support both implicit (ftps) , explicit (ftpes)?

anyway, in case, firewall issue. configured implicit ftp, ports 989, 990, 49152-65535 (per vendor's tech staff). checked network guy , opened ports 20, 21, 989, 990 , 40000-655535 explicit , things worked champ afterwards.

however, in case, appear have connection pool excitement going on. there post on subject here:

how improve performance of ftpwebrequest?

you might want take @ mucking around connection pool set , see if can make progress. hope helps!

regards,

stuart


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

ios - UISlider customization: how to properly add shadow to custom knob image -