![]() If you implement the async dispose pattern for any potential base class, you must provide the protected virtual ValueTask DisposeAsyncCore() method. Implement the async dispose patternĪll nonsealed classes should be considered a potential base class, because they could be inherited. If an implementation of IAsyncDisposable is sealed, the DisposeAsyncCore() method is not needed, and the asynchronous cleanup can be performed directly in the IAsyncDisposable.DisposeAsync() method. The DisposeAsyncCore() method is virtual so that derived classes can define custom cleanup in their overrides. It encapsulates the common asynchronous cleanup operations when a subclass inherits a base class that is an implementation of IAsyncDisposable. The DisposeAsyncCore() method is intended to perform the asynchronous cleanup of managed resources or for cascading calls to DisposeAsync(). Therefore, call Dispose(false) instead of Dispose(true). ![]() In other words, the DisposeAsyncCore() method will dispose of managed resources asynchronously, so you don't want to dispose of them synchronously as well. ![]() This helps ensure functional equivalence with the synchronous dispose pattern, and further ensures that finalizer code paths still get invoked. When implementing the IDisposable.Dispose() method, however, true is passed instead. One primary difference in the async dispose pattern compared to the dispose pattern, is that the call from DisposeAsync() to the Dispose(bool) overload method is given false as an argument. Because of this, it has a standard implementation: public async ValueTask DisposeAsync() Freeing the memory associated with a managed object is always the domain of the garbage collector. The public parameterless DisposeAsync() method is called implicitly in an await using statement, and its purpose is to free unmanaged resources, perform general cleanup, and to indicate that the finalizer, if one is present, need not run. Any nonsealed class should define a DisposeAsyncCore() method that also returns a ValueTask.Ī public IAsyncDisposable.DisposeAsync() implementation that has no parameters.Ī protected virtual ValueTask DisposeAsyncCore() method whose signature is: protected virtual ValueTask DisposeAsyncCore() The IAsyncDisposable interface declares a single parameterless method, DisposeAsync(). Explore DisposeAsync and DisposeAsyncCore methods Specifically, implementations of IDisposable and IAsyncDisposable are properly disposed at the end of their specified lifetime.įor more information, see Dependency injection in. The IServiceProvider and corresponding IHost orchestrate resource cleanup. With regard to dependency injection, when registering services in an IServiceCollection, the service lifetime is managed implicitly on your behalf. This article assumes that you're already familiar with how to implement a Dispose method. All of the guidance for implementing the disposal pattern also applies to the asynchronous implementation. If no synchronous disposable of your class is possible, having only IAsyncDisposable is acceptable. A good implementation pattern of the IAsyncDisposable interface is to be prepared for either synchronous or asynchronous disposal, however, it's not a requirement. It's typical when implementing the IAsyncDisposable interface that classes also implement the IDisposable interface. The DisposeAsync() returns a ValueTask that represents the asynchronous disposal operation. One of the key differences, however, is that this implementation allows for asynchronous cleanup operations. You implement the IAsyncDisposable.DisposeAsync() method when you need to perform resource cleanup, just as you would when implementing a Dispose method. The System.IAsyncDisposable interface was introduced as part of C# 8.0.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |