An established connection was aborted by the software in your host machine

Oct 23, 2012 at 5:57 PM
Edited Oct 31, 2012 at 3:31 AM

I'm using the SftpClient for downloading and uploading files in my Windows Service scheduler program. The class using the SftpClient class is invoked via Reflection. I am encountering the SocketException "An established connection was aborted by the software in your host machine" some time after the invoked method was completed. By then the main thread is in a block state pending next execution, therefore the exception cannot be caught and resulting in an abnormal termination of my service program.

I have read somewhere here it's something to do with the Dispose method when memory resources are reclaimed but so far did not see any solution in the threads browsed.

My initialisation codeas follows:

                if (this._sftpClient == null)
                {
                    ConnectionInfo connectionInfo = new ConnectionInfo(Configuration.HostUrl, Configuration.HostPort, Configuration.Login
                        , new PasswordAuthenticationMethod(Configuration.Login, Utility.Decrypt(Configuration.Password))
                        , new PrivateKeyAuthenticationMethod(Configuration.Login, new PrivateKeyFile(Configuration.PrivateKeyFilePath, Utility.Decrypt(Configuration.PrivateKeyPassPhrase)))
                    );

                    this._sftpClient = new SftpClient(connectionInfo);
                    this._sftpClient.KeepAliveInterval = TimeSpan.FromMinutes(Configuration.KeepAliveInterval);
                    this._sftpClient.OperationTimeout = TimeSpan.FromMinutes(Configuration.Timeout);
                    this._sftpClient.BufferSize = (uint)Configuration.BufferSize;
                    this._sftpClient.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(OnSftpClientErrorOccurred);
                }

                return this._sftpClient;

}

However, the exception did not occur again after I commented out the above highlighted statement which defines the KeepAliveInterval property. Can I assume that this property will keep the socket open even though I called the Disconnect method at the end of the code? Therefore resulting in the exception which cannot be caught?

p/s: I did not call the Dispose method after the Disconnect method. Is it necessary to call the Dispose method as well?

Edit: Dispose method call is required to truly close the connection to the FTP server