11using Microsoft . Extensions . DependencyInjection ;
2+ using System ;
23using System . Collections . Generic ;
4+ using Unity ;
35using Unity . Microsoft . DependencyInjection ;
46using Xunit ;
57
6- namespace UnitTests
8+ namespace Unity . Microsoft . DependencyInjection . Unit . Tests
79{
810 public class ScopedDepencencyTests
911 {
1012 [ 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 ( )
1244 {
1345 // Arrange
1446 var collection = new TestServiceCollection ( )
@@ -18,54 +50,99 @@ public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
1850 var provider = collection . BuildServiceProvider ( ) ;
1951
2052 // Act
21- ITransient transient1 = null ;
22- ITransient transient2a = null ;
53+ ITransient transient1a = null ;
54+ ITransient transient1b = null ;
2355 ITransient transient2b = null ;
2456
2557 using ( var scope1 = provider . CreateScope ( ) )
2658 {
27- transient1 = scope1 . ServiceProvider . GetService < ITransient > ( ) ;
59+ transient1a = scope1 . ServiceProvider . GetService < ITransient > ( ) ;
2860 }
2961
3062 using ( var scope2 = provider . CreateScope ( ) )
3163 {
32- transient2a = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
64+ transient1b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
3365 transient2b = scope2 . ServiceProvider . GetService < ITransient > ( ) ;
3466 }
3567
3668 // 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 ) ;
4173 }
4274
43- private ITransient CreateTransientFactory ( System . IServiceProvider provider )
75+ [ Fact ]
76+ public void ScopedDependencyFromTransientNotSharedAcrossScopes ( )
4477 {
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 ) ;
46106 }
47107
48- public interface ITransient
108+ private ITransient CreateTransientFactory ( IServiceProvider provider )
49109 {
50- IScoped ScopedDependency { get ; }
110+ return provider . GetRequiredService < Transient > ( ) ;
51111 }
112+ }
52113
53- public class Transient : ITransient
114+ public class Foo
115+ {
116+ public Foo ( IServiceProvider sp )
54117 {
55- public Transient ( IScoped scoped )
56- {
57- ScopedDependency = scoped ;
58- }
59-
60- public IScoped ScopedDependency { get ; }
118+ ServiceProvider = sp ;
61119 }
62120
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 ( ) ;
64132
65- public class Scoped : IScoped
133+ public Transient ( IScoped scoped )
66134 {
135+ ScopedDependency = scoped ;
67136 }
68137
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 ( ) ;
69146 }
70147
71148 internal class TestServiceCollection : List < ServiceDescriptor > , IServiceCollection
0 commit comments