File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed
src/Ninject.Web.AspNetCore Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ public object GetRequiredService(Type serviceType)
4242 {
4343 if ( ! IsListType ( serviceType , out var elementType ) )
4444 {
45- return _resolutionRoot . Get ( serviceType ) ;
45+ return _resolutionRoot . Get ( serviceType , metadata => ! HasServiceKeyMetadata ( metadata ) ) ;
4646 }
4747 else
4848 {
@@ -58,7 +58,7 @@ public object GetService(Type serviceType)
5858 object result ;
5959 if ( ! IsListType ( serviceType , out var elementType ) )
6060 {
61- result = _resolutionRoot . TryGet ( serviceType ) ;
61+ result = _resolutionRoot . TryGet ( serviceType , metadata => ! HasServiceKeyMetadata ( metadata ) ) ;
6262 }
6363 else
6464 {
Original file line number Diff line number Diff line change @@ -40,6 +40,9 @@ public IServiceProvider Build()
4040#if NET6_0_OR_GREATER
4141 _kernel . Bind < IServiceProviderIsService > ( ) . To < NinjectServiceProviderIsService > ( ) . InSingletonScope ( ) ;
4242#endif
43+ #if NET8_0_OR_GREATER
44+ _kernel . Bind < IServiceProviderIsKeyedService > ( ) . To < NinjectServiceProviderIsService > ( ) . InSingletonScope ( ) ;
45+ #endif
4346
4447 var adapter = new ServiceCollectionAdapter ( ) ;
4548 adapter . Populate ( _kernel , _services ) ;
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ namespace Ninject.Web.AspNetCore
66{
77#if NET6_0_OR_GREATER
88 public class NinjectServiceProviderIsService : IServiceProviderIsService
9+ #if NET8_0_OR_GREATER
10+ , IServiceProviderIsKeyedService
11+ #endif
912 {
1013 private readonly IKernel _kernel ;
1114
@@ -31,6 +34,26 @@ public bool IsService(Type serviceType)
3134
3235 return _kernel . CanResolve ( serviceType ) ;
3336 }
37+ #if NET8_0_OR_GREATER
38+ public bool IsKeyedService ( Type serviceType , object serviceKey )
39+ {
40+ // IsService should only return true if the type can actually be resolved to a service
41+ // and open generic types cannot. Except for IEnumerable<T> which should return true
42+ // in ANY case (see DependencyInjectionSpecificationTests.IEnumerableWithIsServiceAlwaysReturnsTrue)
43+ if ( serviceType . IsGenericTypeDefinition )
44+ {
45+ return false ;
46+ }
47+
48+ if ( serviceType . IsGenericType && serviceType . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) )
49+ {
50+ return true ;
51+ }
52+
53+ return _kernel . CanResolve ( serviceType , metadata =>
54+ metadata . Get < ServiceKey > ( nameof ( ServiceKey ) ) ? . Key == serviceKey ) ;
55+ }
56+ #endif
3457 }
3558#endif
3659}
You can’t perform that action at this time.
0 commit comments