2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Collections . Generic ;
5
+ using System . Collections . Concurrent ;
6
6
using System . Linq ;
7
7
using System . Threading . Tasks ;
8
8
using DryIoc ;
@@ -16,18 +16,18 @@ public ScopedResolver(IContainer resolver, bool isRootResolver = false)
16
16
{
17
17
Container = resolver ?? throw new ArgumentNullException ( nameof ( resolver ) ) ;
18
18
IsRootResolver = isRootResolver ;
19
- ChildScopes = new HashSet < ServiceScope > ( ) ;
19
+ ChildScopes = new ConcurrentDictionary < ServiceScope , object > ( ) ;
20
20
}
21
21
22
22
public IContainer Container { get ; }
23
23
24
- public HashSet < ServiceScope > ChildScopes { get ; }
24
+ public ConcurrentDictionary < ServiceScope , object > ChildScopes { get ; }
25
25
26
26
public bool IsRootResolver { get ; }
27
27
28
28
public void Dispose ( )
29
29
{
30
- Task childScopeTasks = Task . WhenAll ( ChildScopes . Select ( s => s . DisposalTask ) ) ;
30
+ Task childScopeTasks = Task . WhenAll ( ChildScopes . Keys . Select ( s => s . DisposalTask ) ) ;
31
31
Task . WhenAny ( childScopeTasks , Task . Delay ( 5000 ) )
32
32
. ContinueWith ( t =>
33
33
{
@@ -52,9 +52,9 @@ internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
52
52
} ) ) ;
53
53
54
54
var scope = new ServiceScope ( resolver , scopedRoot ) ;
55
- ChildScopes . Add ( scope ) ;
55
+ ChildScopes . TryAdd ( scope , null ) ;
56
56
57
- scope . DisposalTask . ContinueWith ( t => ChildScopes . Remove ( scope ) ) ;
57
+ scope . DisposalTask . ContinueWith ( t => ChildScopes . TryRemove ( scope , out object _ ) ) ;
58
58
59
59
return scope ;
60
60
}
0 commit comments