Dag
Dag

Reputation: 71

FTP Upload from WinService fails

I have made a small test application(WinForms C#) to test FTP upload. This works perfectly.

I try to use the exact same method in a Windows Service I'm currently working on, but there I get '(426) Connection closed; transfer aborted.'-message.

I have made sure several times that the parameters to the method are exactly the same. They are! Next thought was the account my service is running under, but I've tried all possibilities, even running the service as 'User', supplying my own credentials. Then it should act like the WinForms app, right? No, it doesn't!

It's running fine until the line using (var requestStream = request.GetRequestStream()) which is the point of failure.

The FTP-server in question only allows active connections, so request.UsePassive is set to false.

Anyone got a clue?

       public void UploadToFtp(string url, string filePath, string username, string password, bool mode)
    {
        var fileName = Path.GetFileName(filePath);
        var request = (FtpWebRequest)WebRequest.Create(url + fileName);

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = !mode;
        request.UseBinary = true;
        request.KeepAlive = false;

        using (var fileStream = File.OpenRead(filePath))
        {
            using (var requestStream = request.GetRequestStream())
            {
                fileStream.CopyTo(requestStream);
                requestStream.Close();
            }
        }

        var response = (FtpWebResponse)request.GetResponse();
        response.Close();
    }

Adding trace logs of both scenarios:

Using a Windows application:

 WebRequest::Create(ftp://someurl/somefile.txt)
 FtpWebRequest#63289421::.ctor(ftp://someurl/somefile.txt)
 Exiting WebRequest::Create()   -> FtpWebRequest#63289421
 Current OS installation type is 'Client'.
 RAS supported: True
 ServicePoint#14173886::ServicePoint(someurl:21)
 FtpWebRequest#63289421::GetRequestStream()
 FtpWebRequest#63289421::GetRequestStream(Method=STOR.)
 FtpControlStream#22525719 - Created connection from 10.10.10.103:1865 to nnn.nnn.nnn.nnn:21.
 Associating FtpWebRequest#63289421 with FtpControlStream#22525719
 FtpControlStream#22525719 - Received response [xxxx
   someurl>PROD server
   Port21>Use active mode> 
   xxxx]
 Sending command [USER myusername]
 Received response [331 Enter password]
 Sending command [PASS ********]
 Received response [230-User logged in
 Hi,I'am datagear PROD.
 230 User logged in]
 Sending command [OPTS utf8 on]
 Received response [200 Command OPTS succeed]
 Sending command [PWD]
 Received response [257 "/CitData" is current directory]
 Sending command [TYPE I]
 Received response [200 Transfer mode set to BINARY]
 Sending command [PORT 10,10,10,103,7,74]
 Received response [200 Command PORT succeed]
 Sending command [STOR somefile.txt]
 Received response [150 Uploading in BINARY file somefile.txt]
 Exiting FtpWebRequest#63289421::GetRequestStream() 
 Received response [226 Transfer completed]
 Sending command [QUIT]
 Received response [221-bye
 Bye-Bye,see you again.

Using a Windows Service:

 WebRequest::Create(ftp://someurl/somefile.txt)
 FtpWebRequest#63289421::.ctor(ftp://someurl/somefile.txt)
 Exiting WebRequest::Create()   -> FtpWebRequest#25425822
 Current OS installation type is 'Client'.
 ServicePoint#31665793::ServicePoint(someurl:21)
 FtpWebRequest#25425822::GetRequestStream()
 FtpWebRequest#25425822::GetRequestStream(Method=STOR.)
 FtpControlStream#51484875 - Created connection from 10.10.10.103:1759 to nnn.nnn.nnn.nnn:21.
 Associating FtpWebRequest#25425822 with FtpControlStream#51484875
 FtpControlStream#51484875 - Received response [xxxx
   someurl>PROD server
   Port21>Use active mode> 
   xxxx]
 Sending command [USER myusername]
 Received response [331 Enter password]
 Sending command [PASS ********]
 Received response [230-User logged in
 Hi,I'am datagear PROD.
 230 User logged in]
 Sending command [OPTS utf8 on]
 Received response [200 Command OPTS succeed]
 Sending command [PWD]
 Received response [257 "/CitData" is current directory]
 Sending command [TYPE I]
 Received response [200 Transfer mode set to BINARY]
 Sending command [PORT 10,10,10,103,6,224]
 Received response [200 Command PORT succeed]
 Sending command [STOR somefile.txt]
 Received response [426 Transfer failed]
 (Releasing FTP connection#51484875.)
 GetRequestStream - The remote server returned an error: (426) Connection closed; transfer aborted..
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.CommandStream.Dispose(Boolean disposing)
   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.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetRequestStream()
 Exiting FtpWebRequest#25425822::GetRequestStream()  

The one that works has a line saying 'RAS Supported'. Maybe interesting, don't know.

Upvotes: 3

Views: 957

Answers (1)

Dag
Dag

Reputation: 71

The Windows Firewall caused the problem. When running from a WinService I had to open the firewall for this service. When running from VS environment it seems that the firewall is already open for VS (although the list of pass-through applications in WinFirewall don't show it) and therefore all seems to run well.

Upvotes: 1

Related Questions