Skip to content

Commit 91d1ed2

Browse files
authored
Enable list directory async for net framework (#1206)
* Enable ListDirectoryAsync for .NET Framework * Removed (now) unused constant.
1 parent 1c7166a commit 91d1ed2

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

src/Renci.SshNet.Tests/Classes/SftpClientTest.ListDirectory.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System.Diagnostics;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using Renci.SshNet.Common;
34
using Renci.SshNet.Tests.Properties;
45

5-
using System.Diagnostics;
6-
76
namespace Renci.SshNet.Tests.Classes
87
{
98
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Diagnostics;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Renci.SshNet.Common;
6+
using Renci.SshNet.Tests.Properties;
7+
8+
namespace Renci.SshNet.Tests.Classes
9+
{
10+
/// <summary>
11+
/// Implementation of the SSH File Transfer Protocol (SFTP) over SSH.
12+
/// </summary>
13+
public partial class SftpClientTest
14+
{
15+
[TestMethod]
16+
[TestCategory("Sftp")]
17+
[ExpectedException(typeof(SshConnectionException))]
18+
public async Task Test_Sftp_ListDirectoryAsync_Without_ConnectingAsync()
19+
{
20+
using (var sftp = new SftpClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
21+
{
22+
await foreach (var file in sftp.ListDirectoryAsync(".", CancellationToken.None))
23+
{
24+
Debug.WriteLine(file.FullName);
25+
}
26+
}
27+
}
28+
}
29+
}

src/Renci.SshNet/ISftpClient.cs

-2
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ public interface ISftpClient : IBaseClient, IDisposable
700700
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
701701
IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallback = null);
702702

703-
#if FEATURE_ASYNC_ENUMERABLE
704703
/// <summary>
705704
/// Asynchronously enumerates the files in remote directory.
706705
/// </summary>
@@ -716,7 +715,6 @@ public interface ISftpClient : IBaseClient, IDisposable
716715
/// <exception cref="SshException">A SSH error where <see cref="Exception.Message" /> is the message from the remote host.</exception>
717716
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
718717
IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, CancellationToken cancellationToken);
719-
#endif //FEATURE_ASYNC_ENUMERABLE
720718

721719
/// <summary>
722720
/// Opens a <see cref="SftpFileStream"/> on the specified path with read/write access.

src/Renci.SshNet/Renci.SshNet.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
<PackageReference Include="SshNet.Security.Cryptography" Version="[1.3.0]" />
1414
</ItemGroup>
1515

16+
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
17+
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
18+
</ItemGroup>
19+
1620
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
1721
<DefineConstants>FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
1822
</PropertyGroup>
19-
20-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
21-
<DefineConstants>$(DefineConstants);FEATURE_ASYNC_ENUMERABLE</DefineConstants>
22-
</PropertyGroup>
2323
</Project>

src/Renci.SshNet/SftpClient.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
using Renci.SshNet.Common;
1111
using Renci.SshNet.Sftp;
1212
using System.Threading.Tasks;
13-
#if FEATURE_ASYNC_ENUMERABLE
1413
using System.Runtime.CompilerServices;
15-
#endif
1614

1715
namespace Renci.SshNet
1816
{
@@ -587,7 +585,6 @@ public IEnumerable<ISftpFile> ListDirectory(string path, Action<int> listCallbac
587585
return InternalListDirectory(path, listCallback);
588586
}
589587

590-
#if FEATURE_ASYNC_ENUMERABLE
591588
/// <summary>
592589
/// Asynchronously enumerates the files in remote directory.
593590
/// </summary>
@@ -646,7 +643,6 @@ public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [Enumer
646643
await _sftpSession.RequestCloseAsync(handle, cancellationToken).ConfigureAwait(false);
647644
}
648645
}
649-
#endif //FEATURE_ASYNC_ENUMERABLE
650646

651647
/// <summary>
652648
/// Begins an asynchronous operation of retrieving list of files in remote directory.
@@ -1613,7 +1609,7 @@ public Task<SftpFileStream> OpenAsync(string path, FileMode mode, FileAccess acc
16131609

16141610
cancellationToken.ThrowIfCancellationRequested();
16151611

1616-
return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int)_bufferSize, cancellationToken);
1612+
return SftpFileStream.OpenAsync(_sftpSession, path, mode, access, (int) _bufferSize, cancellationToken);
16171613
}
16181614

16191615
/// <summary>

0 commit comments

Comments
 (0)