From 91d1ed2cbdfacaed242f834c535ad8f2184148bb Mon Sep 17 00:00:00 2001 From: Patrick-3000 <38472350+Patrick-3000@users.noreply.github.com> Date: Sat, 14 Oct 2023 16:35:31 +0200 Subject: [PATCH] Enable list directory async for net framework (#1206) * Enable ListDirectoryAsync for .NET Framework * Removed (now) unused constant. --- .../Classes/SftpClientTest.ListDirectory.cs | 5 ++-- .../SftpClientTest.ListDirectoryAsync.cs | 29 +++++++++++++++++++ src/Renci.SshNet/ISftpClient.cs | 2 -- src/Renci.SshNet/Renci.SshNet.csproj | 8 ++--- src/Renci.SshNet/SftpClient.cs | 6 +--- 5 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectoryAsync.cs diff --git a/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectory.cs b/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectory.cs index ceafd4c50..f44016ecb 100644 --- a/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectory.cs +++ b/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectory.cs @@ -1,9 +1,8 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; using Renci.SshNet.Common; using Renci.SshNet.Tests.Properties; -using System.Diagnostics; - namespace Renci.SshNet.Tests.Classes { /// diff --git a/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectoryAsync.cs b/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectoryAsync.cs new file mode 100644 index 000000000..c800452eb --- /dev/null +++ b/src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectoryAsync.cs @@ -0,0 +1,29 @@ +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Renci.SshNet.Common; +using Renci.SshNet.Tests.Properties; + +namespace Renci.SshNet.Tests.Classes +{ + /// + /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH. + /// + public partial class SftpClientTest + { + [TestMethod] + [TestCategory("Sftp")] + [ExpectedException(typeof(SshConnectionException))] + public async Task Test_Sftp_ListDirectoryAsync_Without_ConnectingAsync() + { + using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD)) + { + await foreach (var file in sftp.ListDirectoryAsync(".", CancellationToken.None)) + { + Debug.WriteLine(file.FullName); + } + } + } + } +} diff --git a/src/Renci.SshNet/ISftpClient.cs b/src/Renci.SshNet/ISftpClient.cs index 29f958d9f..b79a22793 100644 --- a/src/Renci.SshNet/ISftpClient.cs +++ b/src/Renci.SshNet/ISftpClient.cs @@ -700,7 +700,6 @@ public interface ISftpClient : IBaseClient, IDisposable /// The method was called after the client was disposed. IEnumerable ListDirectory(string path, Action listCallback = null); -#if FEATURE_ASYNC_ENUMERABLE /// /// Asynchronously enumerates the files in remote directory. /// @@ -716,7 +715,6 @@ public interface ISftpClient : IBaseClient, IDisposable /// A SSH error where is the message from the remote host. /// The method was called after the client was disposed. IAsyncEnumerable ListDirectoryAsync(string path, CancellationToken cancellationToken); -#endif //FEATURE_ASYNC_ENUMERABLE /// /// Opens a on the specified path with read/write access. diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj index e55911d55..4ccf6c72e 100644 --- a/src/Renci.SshNet/Renci.SshNet.csproj +++ b/src/Renci.SshNet/Renci.SshNet.csproj @@ -13,11 +13,11 @@ + + + + FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP - - - $(DefineConstants);FEATURE_ASYNC_ENUMERABLE - diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index 877fed092..ae8ba29af 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -10,9 +10,7 @@ using Renci.SshNet.Common; using Renci.SshNet.Sftp; using System.Threading.Tasks; -#if FEATURE_ASYNC_ENUMERABLE using System.Runtime.CompilerServices; -#endif namespace Renci.SshNet { @@ -587,7 +585,6 @@ public IEnumerable ListDirectory(string path, Action listCallbac return InternalListDirectory(path, listCallback); } -#if FEATURE_ASYNC_ENUMERABLE /// /// Asynchronously enumerates the files in remote directory. /// @@ -646,7 +643,6 @@ public async IAsyncEnumerable ListDirectoryAsync(string path, [Enumer await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false); } } -#endif //FEATURE_ASYNC_ENUMERABLE /// /// Begins an asynchronous operation of retrieving list of files in remote directory. @@ -1613,7 +1609,7 @@ public Task OpenAsync(string path, FileMode mode, FileAccess acc cancellationToken.ThrowIfCancellationRequested(); - return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken); + return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int) _bufferSize, cancellationToken); } ///