Issue
The method I use is as follow :
public int ExecuteShellCommand(string a_command, bool log = false)
{
int res = 0;
try
{
using (var sshclient = new SshClient(m_connNfo))
{
sshclient.Connect();
using (var cmd = sshclient.CreateCommand(a_command + " >&2"))
{
var resultExe = cmd.Execute();
if (log)
{
Console.Write(resultExe);
var extended = cmd.ExtendedOutputStream;
var reader = new StreamReader(extended);
Console.Write(reader.ReadToEnd());
}
res = cmd.ExitStatus;
}
}
}
catch (Exception)
{
return -1;
}
return res;
}
The m_connNfo is created with the line :
m_connNfo = new ConnectionInfo(ConfigurationManager.AppSettings[IP], 22, User,
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod(User,ApiInstance.GetPass())
});
This works perfectly and do want I expect it to do, the problem is when the '}' of the "try" is reach, so when the using is closed, I get an "System.ObjectDisposedException in System.dll" in the exit terminal.
It also happens when I do :
sshclient.Disconnect();
before closing the using.
I wish to understand why it does that.
Does someone understand why I get this exception ?
Solution
Thanks to Roe and Ralf, I discovered that the error was coming from the dispose of the SshClient. It's an error that comes from an old version of Renci.SshNet 2017, this bug is fixed in the 2020 version.
So to fix this problem, I had to update my RencySsh dll to the newest version.
Answered By - Alex Answer Checked By - Dawn Plyler (WPSolving Volunteer)