简体   繁体   中英

C# Renci.SshNet.Sftp Connect throwing ArgumentNullException

I am using Renci.SshNet.Sftp to connect to SFTP. I am getting the following error when I try to call sftpClientObject.Connect() . Please find below the error. It was working fine couple of days ago. Checked the authentication details and it is perfect.

var _sftpClient = new SftpClient(_hostName, _userName, _password);
using (_sftpClient)
{
    _sftpClient.Connect();
    // Other code to follow
}

Error:
Value cannot be null.
Parameter name: At least one element in the specified array was null.

Exception stacktrace:

   at System.Threading.WaitHandle.WaitAny(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext)
   at System.Threading.WaitHandle.WaitAny(WaitHandle[] waitHandles, TimeSpan timeout, Boolean exitContext)
   at System.Threading.WaitHandle.WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
   at Renci.SshNet.Session.WaitHandle(WaitHandle waitHandle)
   at Renci.SshNet.PasswordAuthenticationMethod.Authenticate(Session session)
   at Renci.SshNet.ConnectionInfo.Authenticate(Session session)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()
   at SftpPoller.FileTransferClient.ProcessFilesInSftp(TextWriter log) in 

But when I use the following code, it worked:

var methods = new AuthenticationMethod[1];
methods[0] = new PasswordAuthenticationMethod(_userName, _password);
var con = new ConnectionInfo(_hostName, _userName, methods) {Timeout = new TimeSpan(0, 0, 0, 60)};
_sftpClient = new SftpClient(con);

Can anyone help me how to solve this issue?

Thanks

It seems that the only imaginable scenario, when the PasswordAuthenticationMethod.Authenticate can pass an uninitialized wait handle to WaitHandle.WaitAny is when the session is closed while the authentication is ongoing.

If the session is closed due to a timeout, setting a higher timeout can resolve the problem, as you have found yourself.

A simpler code to set the timeout is:

var _sftpClient = new SftpClient(_hostName, _userName, _password);
_sftpClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(60);

You seem to be using an old version of SSH.NET (probably the version 2013.4.7 available in NuGet). As the relevant part of the code was refactored in later versions, upgrading might solve this problem too. You should do it anyway as the NuGet SSH.NET package is really old.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM