Skip to content

Add AsyncDisposable support#1005

Merged
pakrym merged 2 commits intomasterfrom
pakrym/AsyncDisposable
Jan 29, 2019
Merged

Add AsyncDisposable support#1005
pakrym merged 2 commits intomasterfrom
pakrym/AsyncDisposable

Conversation

@pakrym
Copy link

@pakrym pakrym commented Jan 27, 2019

Fixes: #426

Implement IAsyncDisposable on ServiceProvider and scopes.

.Dispose throws if there is a service that only implements IAsyncDisposable

@pakrym pakrym requested review from davidfowl and halter73 January 27, 2019 02:41
<value>Implementation type '{0}' can't be converted to service type '{1}'</value>
</data>
<data name="AsyncDisposableServiceDispose" xml:space="preserve">
<value>'{0}' type only implements IAsyncDisposable. Use DisposeAsync to dispose the container.</value>
Copy link
Member

Choose a reason for hiding this comment

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

I'm not 100% sure this is reasonable. There might be cases where something is added to the container out of somebody's control and it'll cause hosting to just crash and burn when the container gets disposed.

Copy link
Author

Choose a reason for hiding this comment

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

They can always DisposeAsync().Wait() to workaround

Copy link
Member

Choose a reason for hiding this comment

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

Hosting is the one calling Dispose on the container.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe make it an option so people can work around it in cases where they don't own the container (like Azure Functions)? By default we can throw but you can turn it off to get the sync over async behavior back.

Copy link
Author

Choose a reason for hiding this comment

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

Both us and Azure Functions are going to move to DisposeAsync() in time for 3.0 and I doubt that there are a lot of types that implement only IAsyncDisposable

Copy link
Member

Choose a reason for hiding this comment

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

Ah true, so I guess this error would never happen unless you explicitly called Dispose on the host.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe TestHost scenarios have the potential to break?

Copy link
Author

Choose a reason for hiding this comment

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

Nope, WebHost.Dispose() blocks StopAsync and provider.DisposeAsync calls would get moved there
https://github.com/aspnet/AspNetCore/blob/master/src/Hosting/Hosting/src/Internal/WebHost.cs#L350

Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't call Dispose in StopAsync. Thats not right. Stopping needs to happen before disposing, not during.

Copy link
Author

@pakrym pakrym Jan 28, 2019

Choose a reason for hiding this comment

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

You are right, Dispose will call DisposeAsync and block.

@pakrym
Copy link
Author

pakrym commented Jan 29, 2019

Seems like we decided that we have a reasonable migration story for customers with current implementation. Merging.

@pakrym pakrym merged commit a58a80b into master Jan 29, 2019
@davidfowl
Copy link
Member

What’s the migration story?

@pakrym
Copy link
Author

pakrym commented Jan 29, 2019

WebHost and TestHost continue working as before, customers would have to change their calls to Dispose to DisposeAsync if they use async disposable only types.

@davidfowl
Copy link
Member

Get this into ASP.NET Core and EF as soon as possible.

@JunTaoLuo JunTaoLuo deleted the pakrym/AsyncDisposable branch February 2, 2019 02:36
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Feb 28, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 2, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 11, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 27, 2020
@ghost ghost locked as resolved and limited conversation to collaborators May 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants