1
1
using Microsoft . Extensions . DependencyInjection ;
2
+ using System ;
2
3
using System . Collections . Generic ;
4
+ using Unity ;
3
5
using Unity . Microsoft . DependencyInjection ;
4
6
using Xunit ;
5
7
6
- namespace UnitTests
8
+ namespace Unity . Microsoft . DependencyInjection . Unit . Tests
7
9
{
8
10
public class ScopedDepencencyTests
9
11
{
10
12
[ Fact ]
11
- public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes ( )
13
+ public void aspnet_Extensions_issues_1301 ( )
14
+ {
15
+ var services = new TestServiceCollection ( )
16
+ . AddSingleton < Foo > ( ) ;
17
+
18
+ var provider = services . BuildServiceProvider ( ) ;
19
+
20
+ IServiceProvider scopedSp1 = null ;
21
+ IServiceProvider scopedSp2 = null ;
22
+ Foo foo1 = null ;
23
+ Foo foo2 = null ;
24
+
25
+ using ( var scope1 = provider . CreateScope ( ) )
26
+ {
27
+ scopedSp1 = scope1 . ServiceProvider ;
28
+ foo1 = scope1 . ServiceProvider . GetRequiredService < Foo > ( ) ;
29
+ }
30
+
31
+ using ( var scope2 = provider . CreateScope ( ) )
32
+ {
33
+ scopedSp2 = scope2 . ServiceProvider ;
34
+ foo2 = scope2 . ServiceProvider . GetRequiredService < Foo > ( ) ;
35
+ }
36
+
37
+ Assert . Equal ( foo1 . ServiceProvider , foo2 . ServiceProvider ) ;
38
+ Assert . NotEqual ( foo1 . ServiceProvider , scopedSp1 ) ;
39
+ Assert . NotEqual ( foo2 . ServiceProvider , scopedSp2 ) ;
40
+ }
41
+
42
+ [ Fact ]
43
+ public void ScopedDependencyFromFactoryNotSharedAcrossScopes ( )
12
44
{
13
45
// Arrange
14
46
var collection = new TestServiceCollection ( )
@@ -18,54 +50,99 @@ public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
18
50
var provider = collection . BuildServiceProvider ( ) ;
19
51
20
52
// Act
21
- ITransient transient1 = null ;
22
- ITransient transient2a = null ;
53
+ ITransient transient1a = null ;
54
+ ITransient transient1b = null ;
23
55
ITransient transient2b = null ;
24
56
25
57
using ( var scope1 = provider . CreateScope ( ) )
26
58
{
27
- transient1 = scope1 . ServiceProvider . GetService < ITransient > ( ) ;
59
+ transient1a = scope1 . ServiceProvider . GetService < ITransient > ( ) ;
28
60
}
29
61
30
62
using ( var scope2 = provider . CreateScope ( ) )
31
63
{
32
- transient2a = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
64
+ transient1b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
33
65
transient2b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
34
66
}
35
67
36
68
// Assert
37
- Assert . NotSame ( transient1 , transient2a ) ;
38
- Assert . NotSame ( transient2a , transient2b ) ;
39
- Assert . NotSame ( transient1 . ScopedDependency , transient2a . ScopedDependency ) ;
40
- Assert . Same ( transient2a . ScopedDependency , transient2b . ScopedDependency ) ;
69
+ Assert . NotSame ( transient1a , transient1b ) ;
70
+ Assert . NotSame ( transient1b , transient2b ) ;
71
+ Assert . NotSame ( transient1a . ScopedDependency , transient1b . ScopedDependency ) ;
72
+ Assert . Same ( transient1b . ScopedDependency , transient2b . ScopedDependency ) ;
41
73
}
42
74
43
- private ITransient CreateTransientFactory ( System . IServiceProvider provider )
75
+ [ Fact ]
76
+ public void ScopedDependencyFromTransientNotSharedAcrossScopes ( )
44
77
{
45
- return provider . GetRequiredService < Transient > ( ) ;
78
+ // Arrange
79
+ var collection = new TestServiceCollection ( )
80
+ . AddTransient < ITransient , Transient > ( )
81
+ . AddScoped < IScoped , Scoped > ( ) ;
82
+
83
+ var provider = collection . BuildServiceProvider ( ) ;
84
+
85
+ // Act
86
+ ITransient transient1a = null ;
87
+ ITransient transient1b = null ;
88
+ ITransient transient2b = null ;
89
+
90
+ using ( var scope1 = provider . CreateScope ( ) )
91
+ {
92
+ transient1a = scope1 . ServiceProvider . GetService < ITransient > ( ) ;
93
+ }
94
+
95
+ using ( var scope2 = provider . CreateScope ( ) )
96
+ {
97
+ transient1b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
98
+ transient2b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
99
+ }
100
+
101
+ // Assert
102
+ Assert . NotSame ( transient1a , transient1b ) ;
103
+ Assert . NotSame ( transient1b , transient2b ) ;
104
+ Assert . NotSame ( transient1a . ScopedDependency , transient1b . ScopedDependency ) ;
105
+ Assert . Same ( transient1b . ScopedDependency , transient2b . ScopedDependency ) ;
46
106
}
47
107
48
- public interface ITransient
108
+ private ITransient CreateTransientFactory ( IServiceProvider provider )
49
109
{
50
- IScoped ScopedDependency { get ; }
110
+ return provider . GetRequiredService < Transient > ( ) ;
51
111
}
112
+ }
52
113
53
- public class Transient : ITransient
114
+ public class Foo
115
+ {
116
+ public Foo ( IServiceProvider sp )
54
117
{
55
- public Transient ( IScoped scoped )
56
- {
57
- ScopedDependency = scoped ;
58
- }
59
-
60
- public IScoped ScopedDependency { get ; }
118
+ ServiceProvider = sp ;
61
119
}
62
120
63
- public interface IScoped { }
121
+ public IServiceProvider ServiceProvider { get ; }
122
+ }
123
+
124
+ public interface ITransient
125
+ {
126
+ IScoped ScopedDependency { get ; }
127
+ }
128
+
129
+ public class Transient : ITransient
130
+ {
131
+ string ID { get ; } = Guid . NewGuid ( ) . ToString ( ) ;
64
132
65
- public class Scoped : IScoped
133
+ public Transient ( IScoped scoped )
66
134
{
135
+ ScopedDependency = scoped ;
67
136
}
68
137
138
+ public IScoped ScopedDependency { get ; }
139
+ }
140
+
141
+ public interface IScoped { }
142
+
143
+ public class Scoped : IScoped
144
+ {
145
+ string ID { get ; } = Guid . NewGuid ( ) . ToString ( ) ;
69
146
}
70
147
71
148
internal class TestServiceCollection : List < ServiceDescriptor > , IServiceCollection
0 commit comments