0

I am attempting to create an SSH client to connect to a custom SSH server. When running the program I receive :

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

frontend code

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

behind code

  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();

        }
0

1 Answer 1

1

The SshClient is being disposed before you return it from the ClientCreate() method. Try this instead:

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.