Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: Disposing client without disconnecting

$
0
0
What is the best practice between these two examples? Does it make sense to put the Disconnect() in a finally block to ensure it is always called or is it acceptable to simply allow dispose to close the channel? I haven't found many examples on the best way to use this library. What is the correct way to handle exceptions after connecting to the SFTP server?
  • When the client is disposed it sends a command to close the channel and hides some internal exceptions.
  • Disconnect() closes the channel and then sends a command to disconnect.
Example 1:
using (var client= new SftpClient(connectionInfo))
{
    client.Connect();
    // There is a possibility that an exception could occur here.
    client.ListDirectory("");
    client.Disconnect();
}
Example 2:
using (var client= new SftpClient(connectionInfo))
{
    try
    {
        client.Connect();
        // There is a possibility that an exception could occur here.
        client.ListDirectory("");
    }
    catch(Exception exception)
    {
        // handle the exception
    }
    finally
    {
        client.Disconnect();
    }
}

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>