SSH.NET [ObjectDisposedException: 无法访问已释放的对象。对象名称:'Renci.SshNet.SshClient'.]

SSH.NET [ObjectDisposedException: Cannot access a disposed object. Object name: 'Renci.SshNet.SshClient'.]

我正在尝试创建 SSH 客户端以连接到自定义 SSH 服务器。当运行我收到的节目时:

Exception Details: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Renci.SshNet.SshClient'.

前端代码

 protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Enabled = true;
        SSHSubmits.SIDSubmitByte();
    }

代码背后

  private static SshClient ClientCreate()

    {
        var Host = "10.0.0.195";
        var Port = int.Parse("42078");
        var Username = "abc";
        var keyFile = new PrivateKeyFile(@"C:\Users\Administrator\.ssh\ACdb_Philos_Div1");
        var keyFiles = new[] { keyFile };
        var methods = new List<AuthenticationMethod>();
        methods.Add(new PrivateKeyAuthenticationMethod(Username, keyFiles));
        var coninfo = new ConnectionInfo(Host, Port, Username, methods.ToArray());
        using (SshClient sshclient = new SshClient(coninfo))
        return sshclient;
    }
    public static void SIDSubmitByte()
    
        {

        ClientCreate().Connect();

        }

SshClient 正在通过 ClientCreate() 方法在您 return 之前处理。试试这个:

private static SshClient ClientCreate()
{
    var Host = "10.0.0.195";
    var Port = int.Parse("42078");
    var Username = "abc";
    var keyFile = new PrivateKeyFile(@"C:\Users\Administrator\.ssh\ACdb_Philos_Div1");
    var keyFiles = new[] { keyFile };
    var methods = new List<AuthenticationMethod>();
    methods.Add(new PrivateKeyAuthenticationMethod(Username, keyFiles));
    var coninfo = new ConnectionInfo(Host, Port, Username, methods.ToArray());
    return new SshClient(coninfo);
}

public static void SIDSubmitByte()
{
    using (var sshClient = ClientCreate())
    {
        sshClient.Connect();
    }
}