Skip to content

Commit 2deb4d0

Browse files
authored
Fix DisposeAsync pattern: remove incorrect Dispose(false) call (#50900)
1 parent 5df1256 commit 2deb4d0

File tree

2 files changed

+1
-8
lines changed

2 files changed

+1
-8
lines changed

docs/standard/garbage-collection/implementing-disposeasync.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,13 @@ The `public` parameterless `DisposeAsync()` method is called implicitly in an `a
4242
public async ValueTask DisposeAsync()
4343
{
4444
// Perform async cleanup.
45-
await DisposeAsyncCore();
46-
47-
// Dispose of unmanaged resources.
48-
Dispose(false);
45+
await DisposeAsyncCore().ConfigureAwait(false);
4946

5047
// Suppress finalization.
5148
GC.SuppressFinalize(this);
5249
}
5350
```
5451

55-
> [!NOTE]
56-
> One primary difference in the async dispose pattern compared to the dispose pattern, is that the call from <xref:System.IAsyncDisposable.DisposeAsync> to the `Dispose(bool)` overload method is given `false` as an argument. When implementing the <xref:System.IDisposable.Dispose?displayProperty=nameWithType> method, however, `true` is passed instead. This helps ensure functional equivalence with the synchronous dispose pattern, and further ensures that finalizer code paths still get invoked. In other words, the `DisposeAsyncCore()` method will dispose of managed resources asynchronously, so you don't want to dispose of them synchronously as well. Therefore, call `Dispose(false)` instead of `Dispose(true)`.
57-
5852
### The `DisposeAsyncCore` method
5953

6054
The `DisposeAsyncCore()` method is intended to perform the asynchronous cleanup of managed resources or for cascading calls to `DisposeAsync()`. It encapsulates the common asynchronous cleanup operations when a subclass inherits a base class that is an implementation of <xref:System.IAsyncDisposable>. The `DisposeAsyncCore()` method is `virtual` so that derived classes can define custom cleanup in their overrides.

docs/standard/garbage-collection/snippets/dispose-async/ExampleConjunctiveDisposable.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public async ValueTask DisposeAsync()
1313
{
1414
await DisposeAsyncCore().ConfigureAwait(false);
1515

16-
Dispose(disposing: false);
1716
GC.SuppressFinalize(this);
1817
}
1918

0 commit comments

Comments
 (0)