Skip to content

OwningComponentBase implements IAsyncDisposable #62583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rolandVi
Copy link

@rolandVi rolandVi commented Jul 7, 2025

OwningComponentBase implements IAsyncDisposable

Description

This pull request enhances the OwningComponentBase class by adding support for asynchronous disposal, improving resource management, and updating tests to validate the new functionality. The most important changes include implementing the IAsyncDisposable interface, adding new methods for asynchronous disposal, updating the public API, and introducing new test cases to ensure proper behavior.

Enhancements to OwningComponentBase:

  • src/Components/Components/src/OwningComponentBase.cs: Updated the OwningComponentBase class to implement the IAsyncDisposable interface. Added DisposeAsync, DisposeAsyncCore, and updated the Dispose method to handle both synchronous and asynchronous disposal of the service scope.
  • The changes were made based on this guidance.

Public API updates:

Testing improvements:

Fixes #25873

@rolandVi rolandVi requested a review from a team as a code owner July 7, 2025 11:36
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Jul 7, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 7, 2025
@ilonatommy ilonatommy added this to the .NET 10 Planning milestone Jul 7, 2025
@ilonatommy ilonatommy added the breaking-change This issue / pr will introduce a breaking change, when resolved / merged. label Jul 7, 2025
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-announcement Indicates that breaking change announcement shuold be posted and linked to this PR label Jul 7, 2025
@rolandVi
Copy link
Author

rolandVi commented Jul 7, 2025

@dotnet-policy-service agree

@ilonatommy ilonatommy removed the community-contribution Indicates that the PR has been added by a community member label Jul 7, 2025
Reverted one change
Roland Vizner and others added 2 commits July 7, 2025 14:54
Removed `OwningComponentBase.DisposeAsync()` from the PublicAPI as the implementation is private now
Comment on lines +47 to +49
/// <summary>
/// Releases the service scope used by the component.
/// </summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sense the AI working here 😄

These methods are not public, so no need to document it. In .NET when a method is an interface implementation, it can use /// <inhertidoc /> to tell it to use the same text as the interface definition.

Comment on lines +64 to +74
if (disposing)
{
if (_scope.HasValue)
{
if (_scope.Value is IDisposable disposable)
{
disposable.Dispose();
}
_scope = null;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably combine the three conditions rather than nesting them:
if(disposing && _scope.HasValue && _scope.Value is IDisposable disposable)

Might be possible to also just do
if(disposing && _scope.Value is IDisposable disposable) (without the has value check). You can set _scope = null unconditionally inside the clause

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

/// Asynchronously releases the service scope used by the component.
/// </summary>
/// <returns>A task that represents the asynchronous dispose operation.</returns>
protected virtual async ValueTask DisposeAsyncCore()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for API review. Since this is a "standard" implementation pattern that people might have followed within their own classes, it might be problematic if we use the same name (as the name will start conflicting) so we should consider "breaking away" from this and using a different name to "avoid the breackage".

Overall, I think this is potentially uncommon since OwningComponentBase is not common in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components breaking-change This issue / pr will introduce a breaking change, when resolved / merged. needs-breaking-change-announcement Indicates that breaking change announcement shuold be posted and linked to this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OwningComponentBase doesn't implement IAsyncDisposable
3 participants