@@ -9,6 +9,7 @@ namespace Manandre.IO
99 /// <summary>
1010 ///
1111 /// </summary>
12+ #pragma warning disable S3881
1213 public abstract class AsyncStream : Stream
1314 {
1415#if ! NETSTANDARD1_3
@@ -286,8 +287,28 @@ public sealed override void Write(byte[] buffer, int offset, int count)
286287 /// </exception>
287288 public abstract override Task WriteAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken ) ;
288289
289- private bool disposed = false ;
290+ #if NETSTANDARD2_1
291+ /// <summary>
292+ /// Asynchronously releases the unmanaged resources used by the FollowingFileStream and optionally
293+ /// releases the managed resources.
294+ /// </summary>
295+ /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.
296+ ///</param>
297+ protected virtual ValueTask DisposeAsync ( bool disposing ) => default ;
298+
299+ /// <summary>
300+ /// Asynchronously releases all resources used by the AsyncStream.
301+ /// </summary>
302+ public sealed override ValueTask DisposeAsync ( ) => DisposeAsync ( true ) ;
290303
304+ /// <summary>
305+ /// Releases the unmanaged resources used by the FollowingFileStream and optionally
306+ /// releases the managed resources.
307+ /// </summary>
308+ /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.
309+ ///</param>
310+ protected sealed override void Dispose ( bool disposing ) => DisposeAsync ( disposing ) . GetAwaiter ( ) . GetResult ( ) ;
311+ #else
291312 /// <summary>
292313 /// Releases the unmanaged resources used by the FollowingFileStream and optionally
293314 /// releases the managed resources.
@@ -296,14 +317,10 @@ public sealed override void Write(byte[] buffer, int offset, int count)
296317 ///</param>
297318 protected override void Dispose ( bool disposing )
298319 {
299- if ( disposed )
300- return ;
301-
302- disposed = true ;
303320 // Call stream class implementation.
304321 base . Dispose ( disposing ) ;
305322 }
306-
323+ #endif
307324 /// <summary>
308325 /// Synchronized version of an async stream
309326 /// </summary>
@@ -400,26 +417,6 @@ public override int WriteTimeout
400417 }
401418 }
402419
403- protected override void Dispose ( bool disposing )
404- {
405- try
406- {
407- // Explicitly pick up a potentially methodimpl'ed Dispose
408- if ( disposing )
409- {
410- cts . Cancel ( ) ;
411- using ( locker . Lock ( ) )
412- {
413- ( ( IDisposable ) _stream ) . Dispose ( ) ;
414- }
415- }
416- }
417- finally
418- {
419- base . Dispose ( disposing ) ;
420- }
421- }
422-
423420 public override long Seek ( long offset , SeekOrigin origin )
424421 {
425422 using ( locker . Lock ( cts . Token ) )
@@ -481,9 +478,64 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
481478 cancellationToken . ThrowIfCancellationRequested ( ) ;
482479 }
483480 }
481+
482+ private bool disposed = false ;
483+
484+ #if NETSTANDARD2_1
485+ protected override async ValueTask DisposeAsync ( bool disposing )
486+ {
487+ if ( disposed )
488+ return ;
489+
490+ try
491+ {
492+ // Explicitly pick up a potentially methodimpl'ed DisposeAsync
493+ if ( disposing )
494+ {
495+ cts . Cancel ( ) ;
496+ using ( await locker . LockAsync ( ) )
497+ {
498+ await ( ( IAsyncDisposable ) _stream ) . DisposeAsync ( ) ;
499+ }
500+ }
501+ }
502+ finally
503+ {
504+ disposed = true ;
505+ await base . DisposeAsync ( disposing ) ;
506+ }
507+ }
508+ #else
509+ protected override void Dispose ( bool disposing )
510+ {
511+ if ( disposed )
512+ return ;
513+
514+ try
515+ {
516+ // Explicitly pick up a potentially methodimpl'ed Dispose
517+ if ( disposing )
518+ {
519+ cts . Cancel ( ) ;
520+ using ( locker . Lock ( ) )
521+ {
522+ ( ( IDisposable ) _stream ) . Dispose ( ) ;
523+ }
524+
525+ }
526+ }
527+ finally
528+ {
529+ disposed = true ;
530+ base . Dispose ( disposing ) ;
531+ }
532+ }
533+ #endif
484534 }
485535 }
486536
537+ #pragma warning restore S3881
538+
487539 /// <summary>
488540 /// AsyncStream class extensions
489541 /// </summary>
0 commit comments