22// Licensed under the MIT License. See License.txt in the project root for license information.
33
44using System ;
5- using System . Collections . Generic ;
5+ using System . Collections . Concurrent ;
66using System . Linq ;
77using System . Threading . Tasks ;
88using DryIoc ;
@@ -16,18 +16,18 @@ public ScopedResolver(IContainer resolver, bool isRootResolver = false)
1616 {
1717 Container = resolver ?? throw new ArgumentNullException ( nameof ( resolver ) ) ;
1818 IsRootResolver = isRootResolver ;
19- ChildScopes = new HashSet < ServiceScope > ( ) ;
19+ ChildScopes = new ConcurrentDictionary < ServiceScope , object > ( ) ;
2020 }
2121
2222 public IContainer Container { get ; }
2323
24- public HashSet < ServiceScope > ChildScopes { get ; }
24+ public ConcurrentDictionary < ServiceScope , object > ChildScopes { get ; }
2525
2626 public bool IsRootResolver { get ; }
2727
2828 public void Dispose ( )
2929 {
30- Task childScopeTasks = Task . WhenAll ( ChildScopes . Select ( s => s . DisposalTask ) ) ;
30+ Task childScopeTasks = Task . WhenAll ( ChildScopes . Keys . Select ( s => s . DisposalTask ) ) ;
3131 Task . WhenAny ( childScopeTasks , Task . Delay ( 5000 ) )
3232 . ContinueWith ( t =>
3333 {
@@ -52,9 +52,9 @@ internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
5252 } ) ) ;
5353
5454 var scope = new ServiceScope ( resolver , scopedRoot ) ;
55- ChildScopes . Add ( scope ) ;
55+ ChildScopes . TryAdd ( scope , null ) ;
5656
57- scope . DisposalTask . ContinueWith ( t => ChildScopes . Remove ( scope ) ) ;
57+ scope . DisposalTask . ContinueWith ( t => ChildScopes . TryRemove ( scope , out object _ ) ) ;
5858
5959 return scope ;
6060 }
0 commit comments