Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

Dispose service instances only once #661

Closed
wants to merge 1 commit into from

Conversation

poke
Copy link

@poke poke commented Oct 22, 2018

This is a fix for aspnet/AspNetCore#2932. Unfortunately, just changing the collection to a hash set would not work as #505 requires this to be an ordered collection. So instead, I’m remembering what services I have disposed at the time they get disposed.

An alternative solution would be to have the hash set around as an instance member to avoid adding duplicate services to the collection. This would have the benefit that services are disposed in the reversed order of their first use. With this fix, they are disposed in the order of their last use. Not sure which of those is better; this version at least avoids having a second collection around all the time.

Make sure that instances that are marked for later disposal are only
disposed once even if they are retrieved through multiple separate ways.
@@ -48,10 +48,12 @@ public void Dispose()
_disposed = true;
if (_disposables != null)
{
var disposed = new HashSet<IDisposable>();
Copy link

@ilmax ilmax Oct 22, 2018

Choose a reason for hiding this comment

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

new HashSet<IDisposable>(_disposables.Count) ?

@natemcmaster
Copy link
Contributor

@poke thanks for the PR. I'm going to close this because we're merging this repo with https://github.com/aspnet/Extensions. After #662 has been merged, please feel free to re-open this PR over there.

cc @davidfowl

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.

None yet

3 participants