Skip to content

Commit 5c29394

Browse files
bad-samaritanMichał DrzymałaRob-Hague
authored
Move AuthenticationMethod IDisposable implementation declaration from inheritees to parent (#746)
* Move IDisposable implementation declaration from inheritees to parent AuthenticationMethod * Move common Dispose code to AuthenticationMethod class * Remove unnecessary finalizers * just move the definition --------- Co-authored-by: Michał Drzymała <[email protected]> Co-authored-by: Rob Hague <[email protected]>
1 parent 7e71bb4 commit 5c29394

8 files changed

+43
-74
lines changed

src/Renci.SshNet/AuthenticationMethod.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Renci.SshNet
77
/// <summary>
88
/// Base class for all supported authentication methods.
99
/// </summary>
10-
public abstract class AuthenticationMethod : IAuthenticationMethod
10+
public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
1111
{
1212
/// <summary>
1313
/// Gets the name of the authentication method.
@@ -61,5 +61,23 @@ AuthenticationResult IAuthenticationMethod.Authenticate(ISession session)
6161
{
6262
return Authenticate((Session)session);
6363
}
64+
65+
/// <summary>
66+
/// Releases unmanaged and - optionally - managed resources.
67+
/// </summary>
68+
/// <param name="disposing">
69+
/// <see langword="true"/> to release both managed and unmanaged resources;
70+
/// <see langword="false"/> to release only unmanaged resources.
71+
/// </param>
72+
protected virtual void Dispose(bool disposing)
73+
{
74+
}
75+
76+
/// <inheritdoc/>
77+
public void Dispose()
78+
{
79+
Dispose(disposing: true);
80+
GC.SuppressFinalize(this);
81+
}
6482
}
6583
}

src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Renci.SshNet
1313
/// <summary>
1414
/// Provides functionality to perform keyboard interactive authentication.
1515
/// </summary>
16-
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable
16+
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod
1717
{
1818
private readonly RequestMessageKeyboardInteractive _requestMessage;
1919
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
@@ -151,20 +151,8 @@ private void Session_UserAuthenticationInformationRequestReceived(object sender,
151151
});
152152
}
153153

154-
/// <summary>
155-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
156-
/// </summary>
157-
public void Dispose()
158-
{
159-
Dispose(disposing: true);
160-
GC.SuppressFinalize(this);
161-
}
162-
163-
/// <summary>
164-
/// Releases unmanaged and - optionally - managed resources.
165-
/// </summary>
166-
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
167-
protected virtual void Dispose(bool disposing)
154+
/// <inheritdoc/>
155+
protected override void Dispose(bool disposing)
168156
{
169157
if (_isDisposed)
170158
{
@@ -182,6 +170,8 @@ protected virtual void Dispose(bool disposing)
182170

183171
_isDisposed = true;
184172
}
173+
174+
base.Dispose(disposing);
185175
}
186176
}
187177
}

src/Renci.SshNet/KeyboardInteractiveConnectionInfo.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,9 @@ protected virtual void Dispose(bool disposing)
162162
{
163163
if (AuthenticationMethods != null)
164164
{
165-
foreach (var authenticationMethods in AuthenticationMethods)
165+
foreach (var authenticationMethod in AuthenticationMethods)
166166
{
167-
if (authenticationMethods is IDisposable disposable)
168-
{
169-
disposable.Dispose();
170-
}
167+
authenticationMethod.Dispose();
171168
}
172169
}
173170

src/Renci.SshNet/NoneAuthenticationMethod.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Renci.SshNet
1010
/// <summary>
1111
/// Provides functionality for "none" authentication method.
1212
/// </summary>
13-
public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
13+
public class NoneAuthenticationMethod : AuthenticationMethod
1414
{
1515
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
1616
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false);
@@ -87,20 +87,8 @@ private void Session_UserAuthenticationFailureReceived(object sender, MessageEve
8787
_ = _authenticationCompleted.Set();
8888
}
8989

90-
/// <summary>
91-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
92-
/// </summary>
93-
public void Dispose()
94-
{
95-
Dispose(disposing: true);
96-
GC.SuppressFinalize(this);
97-
}
98-
99-
/// <summary>
100-
/// Releases unmanaged and - optionally - managed resources.
101-
/// </summary>
102-
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
103-
protected virtual void Dispose(bool disposing)
90+
/// <inheritdoc/>
91+
protected override void Dispose(bool disposing)
10492
{
10593
if (_isDisposed)
10694
{
@@ -118,6 +106,8 @@ protected virtual void Dispose(bool disposing)
118106

119107
_isDisposed = true;
120108
}
109+
110+
base.Dispose(disposing);
121111
}
122112
}
123113
}

src/Renci.SshNet/PasswordAuthenticationMethod.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Renci.SshNet
1313
/// <summary>
1414
/// Provides functionality to perform password authentication.
1515
/// </summary>
16-
public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
16+
public class PasswordAuthenticationMethod : AuthenticationMethod
1717
{
1818
private readonly RequestMessagePassword _requestMessage;
1919
private readonly byte[] _password;
@@ -163,20 +163,8 @@ private void Session_UserAuthenticationPasswordChangeRequiredReceived(object sen
163163
});
164164
}
165165

166-
/// <summary>
167-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
168-
/// </summary>
169-
public void Dispose()
170-
{
171-
Dispose(disposing: true);
172-
GC.SuppressFinalize(this);
173-
}
174-
175-
/// <summary>
176-
/// Releases unmanaged and - optionally - managed resources.
177-
/// </summary>
178-
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
179-
protected virtual void Dispose(bool disposing)
166+
/// <inheritdoc/>
167+
protected override void Dispose(bool disposing)
180168
{
181169
if (_isDisposed)
182170
{
@@ -194,6 +182,8 @@ protected virtual void Dispose(bool disposing)
194182

195183
_isDisposed = true;
196184
}
185+
186+
base.Dispose(disposing);
197187
}
198188
}
199189
}

src/Renci.SshNet/PasswordConnectionInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,7 @@ protected virtual void Dispose(bool disposing)
281281
{
282282
foreach (var authenticationMethod in AuthenticationMethods)
283283
{
284-
if (authenticationMethod is IDisposable disposable)
285-
{
286-
disposable.Dispose();
287-
}
284+
authenticationMethod.Dispose();
288285
}
289286
}
290287

src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Renci.SshNet
1313
/// <summary>
1414
/// Provides functionality to perform private key authentication.
1515
/// </summary>
16-
public class PrivateKeyAuthenticationMethod : AuthenticationMethod, IDisposable
16+
public class PrivateKeyAuthenticationMethod : AuthenticationMethod
1717
{
1818
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
1919
private EventWaitHandle _authenticationCompleted = new ManualResetEvent(initialState: false);
@@ -155,20 +155,8 @@ private void Session_UserAuthenticationPublicKeyReceived(object sender, MessageE
155155
_ = _authenticationCompleted.Set();
156156
}
157157

158-
/// <summary>
159-
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
160-
/// </summary>
161-
public void Dispose()
162-
{
163-
Dispose(disposing: true);
164-
GC.SuppressFinalize(this);
165-
}
166-
167-
/// <summary>
168-
/// Releases unmanaged and - optionally - managed resources.
169-
/// </summary>
170-
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
171-
protected virtual void Dispose(bool disposing)
158+
/// <inheritdoc/>
159+
protected override void Dispose(bool disposing)
172160
{
173161
if (_isDisposed)
174162
{
@@ -186,6 +174,8 @@ protected virtual void Dispose(bool disposing)
186174

187175
_isDisposed = true;
188176
}
177+
178+
base.Dispose(disposing);
189179
}
190180

191181
private sealed class SignatureData : SshData

src/Renci.SshNet/PrivateKeyConnectionInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ protected virtual void Dispose(bool disposing)
160160
{
161161
foreach (var authenticationMethod in AuthenticationMethods)
162162
{
163-
if (authenticationMethod is IDisposable disposable)
164-
{
165-
disposable.Dispose();
166-
}
163+
authenticationMethod.Dispose();
167164
}
168165
}
169166

0 commit comments

Comments
 (0)