Getting 'System.ObjectDisposedException' in System.dll in the exit terminal when disconnecting the sshclient with Renci.SshNet

huangapple go评论108阅读模式
英文:

Getting 'System.ObjectDisposedException' in System.dll in the exit terminal when disconnecting the sshclient with Renci.SshNet

问题

以下是翻译好的代码部分:

我使用的方法如下:

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;
}

m_connNfo是使用以下行创建的

m_connNfo = new ConnectionInfo(ConfigurationManager.AppSettings[IP], 22, User,
    new AuthenticationMethod[]{
        // 基于密码的身份验证
        new PasswordAuthenticationMethod(User,ApiInstance.GetPass())
         });

请注意,这是您提供的代码的翻译部分,不包括问题或其他内容。

英文:

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 ?

答案1

得分: 1

感谢Roe和Ralf,我发现错误是来自于SshClient的dispose。
这是一个来自于Renci.SshNet 2017旧版本的错误,这个bug在2020版本中已经修复

所以为了解决这个问题,我不得不将我的RenciSsh dll更新到最新版本。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月25日 17:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定