Open
Description
Background and Motivation
OwningComponentBase currently only implements IDisposable for cleaning up its service scope. However, when the service scope contains async-disposable services, the synchronous disposal pattern doesn't allow for proper async cleanup. This change adds IAsyncDisposable support to OwningComponentBase to ensure that async-disposable services within the component's service scope can be properly cleaned up using async disposal patterns.
Proposed API
namespace Microsoft.AspNetCore.Components
{
public abstract class OwningComponentBase : ComponentBase, IDisposable
+ , IAsyncDisposable
{
+ protected virtual ValueTask DisposeAsyncCore();
}
}
Usage Examples
// Component that uses async-disposable services
public class MyComponent : OwningComponentBase<IMyAsyncDisposableService>
{
protected override async Task OnInitializedAsync()
{
// Use the service
await Service.DoSomethingAsync();
}
}
// Service that implements IAsyncDisposable
public class MyAsyncDisposableService : IMyAsyncDisposableService, IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
// Async cleanup logic
await CleanupResourcesAsync();
}
}
// The component will now properly dispose async services when disposed
@page "/mypage"
@inherits OwningComponentBase<IMyAsyncDisposableService>
@* Component content *@
Alternative Designs
N/A - The implementation follows the standard .NET pattern for implementing both IDisposable and IAsyncDisposable on the same type, as documented in the .NET guidelines.
Risks
The change maintains backward compatibility by:
- Keeping the existing IDisposable implementation intact
- Using default interface implementation patterns
- Following the standard dispose pattern with GC.SuppressFinalize
No breaking changes are introduced as the existing synchronous disposal behavior is preserved.