@@ -78,6 +78,71 @@ public void ExistingMultipleServices_ResolvesNonKeyedToNull()
7878 provider . GetService ( typeof ( IWarrior ) ) . Should ( ) . BeNull ( ) ;
7979 }
8080
81+ [ Fact ]
82+ public void RequiredKeyedServiceCollectionExisting_CorrectServiceResolved ( )
83+ {
84+ var collection = new ServiceCollection ( ) ;
85+ collection . Add ( new ServiceDescriptor ( typeof ( IWarrior ) , "Samurai" , typeof ( Samurai ) , ServiceLifetime . Transient ) ) ;
86+ collection . Add ( new ServiceDescriptor ( typeof ( IWarrior ) , "Ninja" , new Ninja ( "test" ) ) ) ;
87+ var kernel = CreateTestKernel ( collection ) ;
88+ var provider = CreateServiceProvider ( kernel ) ;
89+
90+ provider . GetRequiredKeyedService ( typeof ( IWarrior ) , "Ninja" ) . Should ( ) . NotBeNull ( ) . And . BeOfType ( typeof ( Ninja ) ) ;
91+ provider . GetRequiredKeyedService ( typeof ( IWarrior ) , "Samurai" ) . Should ( ) . NotBeNull ( ) . And . BeOfType ( typeof ( Samurai ) ) ;
92+
93+ }
94+
95+ [ Fact ]
96+ public void RequiredKeyedNinjectDirectBindingExisting_CorrectServiceResolved ( )
97+ {
98+ var kernel = CreateTestKernel ( ) ;
99+ kernel . Bind < IWarrior > ( ) . To < Samurai > ( ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Samurai" ) ) ;
100+ kernel . Bind < IWarrior > ( ) . ToConstant ( new Ninja ( "test" ) ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Ninja" ) ) ;
101+ var provider = CreateServiceProvider ( kernel ) ;
102+
103+ provider . GetRequiredKeyedService ( typeof ( IWarrior ) , "Samurai" ) . Should ( ) . NotBeNull ( ) . And . BeOfType ( typeof ( Samurai ) ) ;
104+ provider . GetRequiredKeyedService ( typeof ( IWarrior ) , "Ninja" ) . Should ( ) . NotBeNull ( ) . And . BeOfType ( typeof ( Ninja ) ) ;
105+ }
106+
107+ [ Fact ]
108+ public void RequiredKeyedNonExisting_SingleServiceResolvedToException ( )
109+ {
110+ var kernel = CreateTestKernel ( ) ;
111+ var provider = CreateServiceProvider ( kernel ) ;
112+
113+ Action action = ( ) => provider . GetRequiredKeyedService ( typeof ( IWarrior ) , "Samurai" ) ;
114+ action . Should ( ) . Throw < ActivationException > ( ) . WithMessage ( "*No matching bindings are available*" ) ;
115+ }
116+
117+ [ Fact ]
118+ public void RequiredExistingMultipleKeydServices_ResolvedQueriedAsList ( )
119+ {
120+ var kernel = CreateTestKernel ( ) ;
121+ kernel . Bind < IWarrior > ( ) . To < Samurai > ( ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Samurai" ) ) ; ;
122+ kernel . Bind < IWarrior > ( ) . ToConstant ( new Ninja ( "test" ) ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Ninja" ) ) ;
123+ var provider = CreateServiceProvider ( kernel ) ;
124+
125+ var result = provider . GetRequiredService ( typeof ( IList < IWarrior > ) ) as IEnumerable < IWarrior > ;
126+
127+ result . Should ( ) . NotBeNull ( ) ;
128+ var resultList = result . ToList ( ) ;
129+ resultList . Should ( ) . HaveCount ( 2 ) ;
130+ resultList . Should ( ) . Contain ( x => x is Samurai ) ;
131+ resultList . Should ( ) . Contain ( x => x is Ninja ) ;
132+ }
133+
134+ [ Fact ]
135+ public void ExistingMultipleServices_ResolvesNonKeyedToException ( )
136+ {
137+ var kernel = CreateTestKernel ( ) ;
138+ kernel . Bind < IWarrior > ( ) . To < Samurai > ( ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Samurai" ) ) ; ;
139+ kernel . Bind < IWarrior > ( ) . ToConstant ( new Ninja ( "test" ) ) . WithMetadata ( nameof ( ServiceKey ) , new ServiceKey ( "Ninja" ) ) ;
140+ var provider = CreateServiceProvider ( kernel ) ;
141+
142+ Action action = ( ) => provider . GetRequiredService ( typeof ( IWarrior ) ) ;
143+ action . Should ( ) . Throw < ActivationException > ( ) . WithMessage ( "*More than one matching bindings are available*" ) ;
144+ }
145+
81146 private IServiceProvider CreateServiceProvider ( AspNetCoreKernel kernel )
82147 {
83148 NinjectServiceProviderBuilder builder = CreateServiceProviderBuilder ( kernel ) ;
0 commit comments