-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implemented IAsyncDisposable for WebApplicationFactory #29456
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
Thanks for taking care of this so quick!
@javiercn My pleasure! |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
/AzurePipelines run AspNetCore-ci |
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously | ||
/// </summary> | ||
protected virtual async ValueTask DisposeAsyncCore() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need to add this API. I think it's sufficient to explicitly implement IAsyncDisposable
in derived class and call base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean, I should remove this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then dispose will be synchronous! Because public method DisposeAsync()
will call sync version of dispose on line 538.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pranavkm I have removed DisposeAsyncCore()
method in the latest commit. But right now DisposeAsync()
method calling the synchronous version of Dispose()
under the hood, isn't that wrong? Or am I missing something here to understand!
Thanks!
1. Removed DisposeAsyncCore method
Coming back to this, I think the pattern should be something like this, based on: The main points are:
@davidfowl thoughts? public class MyClass : IDisposable, IAsyncDisposable
{
private bool _disposed = false;
private bool _disposeAsync = false;
public void Dispose()
{
if(_disposed)
{
return;
}
Dispose(disposing: true);
GC.SupressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(disposing)
{
if(!_disposeAsync)
{
DisposeAsyncCore()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
}
}
// Release unmanaged resources
}
public virtual ValueTask DisposeAsyncCore() => return default;
public ValueTask DisposeAsync()
{
if(_disposed)
{
return;
}
if(_disposeAsync)
{
return;
}
_disposed = true;
await DisposeAsyncCore().ConfigureAwait(false);
_disposeAsync = true;
Dispose(disposing: true);
GC.SupressFinalize(this);
}
~MyClass
{
Dispose(disposing: false);
}
} |
public ValueTask DisposeAsync() | ||
{ | ||
Dispose(disposing: false); | ||
GC.SuppressFinalize(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this, there's no finalizer.
/// <inheritdoc /> | ||
public ValueTask DisposeAsync() | ||
{ | ||
Dispose(disposing: false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to actually call DisposeAsync on the host.
public async Task TestingInfrastructure_GenericHost_HostDisposeAsync() | ||
{ | ||
// Act | ||
await using var factory = new CustomizedFactory<GenericHostWebSite.Startup>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure this adds a service that is only IAsyncDisposable, that's the real test.
Hi @MCCshreyas. |
Fixes #29455