From 01978a6052496fb271278a853db13232d1fefa90 Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Thu, 8 Aug 2024 15:12:21 +0200 Subject: [PATCH 1/2] Drop net7.0 target .NET 7 is EOL since May. The only .NET 7 features we use are `ObjectDisposedException.ThrowIf` (moved to a throw helper) and some newer regex features. This feels a bit weird, but I suppose it is the expected course of action. --- src/Renci.SshNet/BaseClient.cs | 11 ++------ src/Renci.SshNet/Common/ChannelInputStream.cs | 10 +------- src/Renci.SshNet/Common/PipeStream.cs | 14 +---------- src/Renci.SshNet/Common/ThrowHelper.cs | 25 +++++++++++++++++++ src/Renci.SshNet/ForwardedPortDynamic.cs | 9 +------ src/Renci.SshNet/ForwardedPortLocal.cs | 9 +------ src/Renci.SshNet/ForwardedPortRemote.cs | 9 +------ src/Renci.SshNet/Renci.SshNet.csproj | 2 +- src/Renci.SshNet/Sftp/SftpFileReader.cs | 9 +------ src/Renci.SshNet/Sftp/SftpFileStream.cs | 9 +------ src/Renci.SshNet/ShellStream.cs | 16 ++---------- src/Renci.SshNet/SshCommand.cs | 9 +------ src/Renci.SshNet/SubsystemSession.cs | 18 +++---------- .../Renci.SshNet.Tests.csproj | 2 +- 14 files changed, 42 insertions(+), 110 deletions(-) create mode 100644 src/Renci.SshNet/Common/ThrowHelper.cs diff --git a/src/Renci.SshNet/BaseClient.cs b/src/Renci.SshNet/BaseClient.cs index bf728abc0..0a311125e 100644 --- a/src/Renci.SshNet/BaseClient.cs +++ b/src/Renci.SshNet/BaseClient.cs @@ -448,17 +448,10 @@ protected virtual void Dispose(bool disposing) /// /// Check if the current instance is disposed. /// - /// THe current instance is disposed. + /// The current instance is disposed. protected void CheckDisposed() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); } /// diff --git a/src/Renci.SshNet/Common/ChannelInputStream.cs b/src/Renci.SshNet/Common/ChannelInputStream.cs index b7e4be241..31d02b010 100644 --- a/src/Renci.SshNet/Common/ChannelInputStream.cs +++ b/src/Renci.SshNet/Common/ChannelInputStream.cs @@ -116,10 +116,7 @@ public override void Write(byte[] buffer, int offset, int count) throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative."); } - if (_isDisposed) - { - throw CreateObjectDisposedException(); - } + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); if (count == 0) { @@ -208,10 +205,5 @@ public override long Position get { return _totalPosition; } set { throw new NotSupportedException(); } } - - private ObjectDisposedException CreateObjectDisposedException() - { - return new ObjectDisposedException(GetType().FullName); - } } } diff --git a/src/Renci.SshNet/Common/PipeStream.cs b/src/Renci.SshNet/Common/PipeStream.cs index 61a7f3bda..a2d09fefb 100644 --- a/src/Renci.SshNet/Common/PipeStream.cs +++ b/src/Renci.SshNet/Common/PipeStream.cs @@ -90,7 +90,7 @@ public override void Write(byte[] buffer, int offset, int count) { lock (_sync) { - ThrowIfDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_disposed, this); AssertValid(); @@ -232,17 +232,5 @@ public override long Position get { return 0; } set { throw new NotSupportedException(); } } - - private void ThrowIfDisposed() - { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_disposed, this); -#else - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER - } } } diff --git a/src/Renci.SshNet/Common/ThrowHelper.cs b/src/Renci.SshNet/Common/ThrowHelper.cs new file mode 100644 index 000000000..002bb42f5 --- /dev/null +++ b/src/Renci.SshNet/Common/ThrowHelper.cs @@ -0,0 +1,25 @@ +#nullable enable +using System; + +namespace Renci.SshNet.Common +{ + internal static class ThrowHelper + { + public static void ThrowObjectDisposedIf(bool condition, object instance) + { +#if NET7_0_OR_GREATER + ObjectDisposedException.ThrowIf(condition, instance); +#else + if (condition) + { + Throw(instance); + + static void Throw(object? instance) + { + throw new ObjectDisposedException(instance?.GetType().FullName); + } + } +#endif + } + } +} diff --git a/src/Renci.SshNet/ForwardedPortDynamic.cs b/src/Renci.SshNet/ForwardedPortDynamic.cs index f09d73369..9ee6da69d 100644 --- a/src/Renci.SshNet/ForwardedPortDynamic.cs +++ b/src/Renci.SshNet/ForwardedPortDynamic.cs @@ -128,14 +128,7 @@ protected override void StopPort(TimeSpan timeout) /// The current instance is disposed. protected override void CheckDisposed() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); } /// diff --git a/src/Renci.SshNet/ForwardedPortLocal.cs b/src/Renci.SshNet/ForwardedPortLocal.cs index 1a34a5d9f..d1f465ed4 100644 --- a/src/Renci.SshNet/ForwardedPortLocal.cs +++ b/src/Renci.SshNet/ForwardedPortLocal.cs @@ -164,14 +164,7 @@ protected override void StopPort(TimeSpan timeout) /// The current instance is disposed. protected override void CheckDisposed() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); } /// diff --git a/src/Renci.SshNet/ForwardedPortRemote.cs b/src/Renci.SshNet/ForwardedPortRemote.cs index 17a1a569b..361936feb 100644 --- a/src/Renci.SshNet/ForwardedPortRemote.cs +++ b/src/Renci.SshNet/ForwardedPortRemote.cs @@ -228,14 +228,7 @@ protected override void StopPort(TimeSpan timeout) /// The current instance is disposed. protected override void CheckDisposed() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); } private void Session_ChannelOpening(object sender, MessageEventArgs e) diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj index bea1b66af..961d5526e 100644 --- a/src/Renci.SshNet/Renci.SshNet.csproj +++ b/src/Renci.SshNet/Renci.SshNet.csproj @@ -4,7 +4,7 @@ Renci.SshNet SSH.NET SSH.NET - net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0 + net462;netstandard2.0;netstandard2.1;net6.0;net8.0 diff --git a/src/Renci.SshNet/Sftp/SftpFileReader.cs b/src/Renci.SshNet/Sftp/SftpFileReader.cs index f47a941d9..57bcf2985 100644 --- a/src/Renci.SshNet/Sftp/SftpFileReader.cs +++ b/src/Renci.SshNet/Sftp/SftpFileReader.cs @@ -74,14 +74,7 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i public byte[] Read() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_disposingOrDisposed, this); -#else - if (_disposingOrDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_disposingOrDisposed, this); if (_exception is not null) { diff --git a/src/Renci.SshNet/Sftp/SftpFileStream.cs b/src/Renci.SshNet/Sftp/SftpFileStream.cs index 8bf0f864a..5e00c3dbd 100644 --- a/src/Renci.SshNet/Sftp/SftpFileStream.cs +++ b/src/Renci.SshNet/Sftp/SftpFileStream.cs @@ -1364,14 +1364,7 @@ private void SetupWrite() private void CheckSessionIsOpen() { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_session is null, this); -#else - if (_session is null) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER + ThrowHelper.ThrowObjectDisposedIf(_session is null, this); if (!_session.IsOpen) { diff --git a/src/Renci.SshNet/ShellStream.cs b/src/Renci.SshNet/ShellStream.cs index d7228229c..40325a0bb 100644 --- a/src/Renci.SshNet/ShellStream.cs +++ b/src/Renci.SshNet/ShellStream.cs @@ -231,7 +231,7 @@ public override bool CanWrite /// public override void Flush() { - ThrowIfDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_disposed, this); Debug.Assert(_writeLength >= 0 && _writeLength <= _writeBuffer.Length); @@ -766,18 +766,6 @@ private static void ValidateLookback(int lookback) } } - private void ThrowIfDisposed() - { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_disposed, this); -#else - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER - } - /// /// Reads all of the text currently available in the shell. /// @@ -847,7 +835,7 @@ public void Write(string? text) /// public override void Write(byte[] buffer, int offset, int count) { - ThrowIfDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_disposed, this); while (count > 0) { diff --git a/src/Renci.SshNet/SshCommand.cs b/src/Renci.SshNet/SshCommand.cs index f7f1d3dfd..b8a08b892 100644 --- a/src/Renci.SshNet/SshCommand.cs +++ b/src/Renci.SshNet/SshCommand.cs @@ -254,14 +254,7 @@ internal SshCommand(ISession session, string commandText, Encoding encoding) #pragma warning disable CA1849 // Call async methods when in an async method; PipeStream.DisposeAsync would complete synchronously anyway. public Task ExecuteAsync(CancellationToken cancellationToken = default) { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); if (cancellationToken.IsCancellationRequested) { diff --git a/src/Renci.SshNet/SubsystemSession.cs b/src/Renci.SshNet/SubsystemSession.cs index 70385d903..528f5dcae 100644 --- a/src/Renci.SshNet/SubsystemSession.cs +++ b/src/Renci.SshNet/SubsystemSession.cs @@ -57,7 +57,7 @@ internal IChannelSession Channel { get { - EnsureNotDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); return _channel; } @@ -106,7 +106,7 @@ protected SubsystemSession(ISession session, string subsystemName, int operation /// The channel session could not be opened, or the subsystem could not be executed. public void Connect() { - EnsureNotDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); if (IsOpen) { @@ -166,7 +166,7 @@ public void Disconnect() /// The data to be sent. public void SendData(byte[] data) { - EnsureNotDisposed(); + ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this); EnsureSessionIsOpen(); _channel.SendData(data); @@ -544,17 +544,5 @@ protected virtual void Dispose(bool disposing) { Dispose(disposing: false); } - - private void EnsureNotDisposed() - { -#if NET7_0_OR_GREATER - ObjectDisposedException.ThrowIf(_isDisposed, this); -#else - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } -#endif // NET7_0_OR_GREATER - } } } diff --git a/test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj b/test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj index 08b744b39..ac55bd11f 100644 --- a/test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj +++ b/test/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj @@ -1,7 +1,7 @@  - net462;net6.0;net7.0;net8.0 + net462;net6.0;net8.0 From 4f1bdb475dbcd14b4d919c603647f9cec4dd534f Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Mon, 9 Dec 2024 20:54:30 +0100 Subject: [PATCH 2/2] fix build warning-as-error which is suddenly appearing on net6.0 IsAotCompatible not supported on net6.0 --- src/Renci.SshNet/Renci.SshNet.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj index 5daf013c7..ff3237a10 100644 --- a/src/Renci.SshNet/Renci.SshNet.csproj +++ b/src/Renci.SshNet/Renci.SshNet.csproj @@ -31,6 +31,10 @@ + true + + + true