浏览代码

Enable list directory async for net framework (#1206)

* Enable ListDirectoryAsync for .NET Framework
* Removed (now) unused constant.
Patrick-3000 2 年之前
父节点
当前提交
91d1ed2cbd

+ 2 - 3
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
 {
     /// <summary>

+ 29 - 0
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
+{
+    /// <summary>
+    /// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
+    /// </summary>
+    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);
+                }
+            }
+        }
+    }
+}

+ 0 - 2
src/Renci.SshNet/ISftpClient.cs

@@ -700,7 +700,6 @@ namespace Renci.SshNet
         /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
         IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallback = null);
 
-#if FEATURE_ASYNC_ENUMERABLE
         /// <summary>
         /// Asynchronously enumerates the files in remote directory.
         /// </summary>
@@ -716,7 +715,6 @@ namespace Renci.SshNet
         /// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
         /// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
         IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, CancellationToken cancellationToken);
-#endif //FEATURE_ASYNC_ENUMERABLE
 
         /// <summary>
         /// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.

+ 4 - 4
src/Renci.SshNet/Renci.SshNet.csproj

@@ -13,11 +13,11 @@
     <PackageReference Include="SshNet.Security.Cryptography" Version="[1.3.0]" />
   </ItemGroup>
 
+  <ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
+    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
+  </ItemGroup>
+
   <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
     <DefineConstants>FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
   </PropertyGroup>
-
-  <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
-    <DefineConstants>$(DefineConstants);FEATURE_ASYNC_ENUMERABLE</DefineConstants>
-  </PropertyGroup>
 </Project>

+ 1 - 5
src/Renci.SshNet/SftpClient.cs

@@ -10,9 +10,7 @@ using Renci.SshNet.Abstractions;
 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 @@ namespace Renci.SshNet
             return InternalListDirectory(path, listCallback);
         }
 
-#if FEATURE_ASYNC_ENUMERABLE
         /// <summary>
         /// Asynchronously enumerates the files in remote directory.
         /// </summary>
@@ -646,7 +643,6 @@ namespace Renci.SshNet
                 await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false);
             }
         }
-#endif //FEATURE_ASYNC_ENUMERABLE
 
         /// <summary>
         /// Begins an asynchronous operation of retrieving list of files in remote directory.
@@ -1613,7 +1609,7 @@ namespace Renci.SshNet
 
             cancellationToken.ThrowIfCancellationRequested();
 
-            return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken);
+            return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int) _bufferSize, cancellationToken);
         }
 
         /// <summary>