+1 vote
by (130 points)

Hi Team,
when i copy the file from internal server to external server i am calling the Rebex.. and when moving the file my server connection getting disconnected, because of this the file is not moving to the external server and its skip the file. could you plz provide how to handle the reattempt process to move the file to external server...

here is my code.

            if (string.IsNullOrEmpty(pwd))
            {
                rebexSftp.Login(sftpFileInfo.SftpLoginUser, privateKey);
            }
            else
            {
                rebexSftp.Login(sftpFileInfo.SftpLoginUser, pwd, privateKey);
            }

            /////////////////////////////////////
            textWriter.WriteLine("Login successful");
            /////////////////////////////////////

            rebexSftp.PutFile(localfile, remotefile);

            /////////////////////////////////////
            textWriter.WriteLine("PutFile successful");
            /////////////////////////////////////

            rebexSftp.Disconnect();
Applies to: Rebex SFTP

1 Answer

0 votes
by (58.9k points)
edited by

Retrying a failed file upload for a specified number of times has already been discussed in this forum answer.

For your convenience, here is the code that retries the PutFile method 3 times before giving up. The code also handles reconnecting to the server if needed:

int cnt = 0;
int maxRetry = 3;

Sftp sftp = new Sftp();
sftp.Connect(_server);
sftp.Login(_user, _password);

while (cnt <= maxRetry)
{
    try
    {
        sftp.PutFile(@"localPathToFile", "remotePathToFile");
        sftp.Disconnect();
        break;
    }
    catch (NetworkSessionException ex)
    {
        // sftp.Disconnect();
        if (cnt == maxRetry)
            throw;

        if (ex.Status != NetworkSessionExceptionStatus.ProtocolError)
        {
            // reconnect needed if the status is other than ProtocolError
            sftp = new Sftp();
            sftp.Connect(_server);
            sftp.Login(_user, _password);
        }

        cnt++;
    }
}
by (58.9k points)
Please give it a try and let me know whether it solves your problem
by (130 points)
hi thanks for your quick reply. i will test this and let you know.....
by (130 points)
Hi tomas,
                  am getting the timeout exception when i move the file from one to another server..  

Error : 1.An error occurred sending a file over SFTP. Timeout exceeded while waiting for file replication.....
2. An error occurred sending a file over SFTP. The process cannot access the file 'D:\..\Logs\EFT_20150713_092417.txt' because it is being used by another process.
3. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
by (58.9k points)
Hi kasi5a,

Error 1: "Timeout exceeded while waiting for file replication":
does not come from Rebex components, so whether it comes from your code or from somewhere else it is up to you to figure out why the error occured and what it means.

Error 2: "The process cannot access the file 'D:\..\Logs\EFT_20150713_092417.txt' because it is being used by another process.":

Actually the error could have been thrown by Sftp.PutFile method. However, it comes from .NET itself. This can be reproduced easily if you try to upload a file that is "locked". However the error message is misleading - actually even the same process (the program) could cause the exception to be thrown. Please see this code that reproduces the error:

       // create a text file
       StreamWriter writer = File.CreateText("sample.txt");
       writer.WriteLine("This is a sample file.");

       // if the file is not closed (or finalized by the garbage collector),
       // the following code will throw the "The process cannot access the file" exception

       // connect to the server and upload the file
       var sftp = new Sftp();
       sftp.Connect("server");
       sftp.Login(username, password);
       sftp.PutFile("sample.txt", "sample.txt");
       sftp.Disconnect();
       
To overcome the error please make sure that our component will be able to read from the file. In my code above it means closing the StreamWriter by calling writer.Close(); before performing the Sftp.PutFile method

Error 3: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond." is not thrown by the PutFile method and it has already been discussed at http://forum.rebex.net/1848/connection-connected-properly-established-connection-connected
...