From 96e2fe04a083efcd1e09d1da1afa9786d378352f Mon Sep 17 00:00:00 2001 From: rizi Date: Mon, 18 Nov 2024 18:37:51 +0100 Subject: [PATCH] Use 'keyed service' code only for .net 8 and newer versions --- ...closed_generic_interface_registration_check.cs | 4 +++- .../IKeyedServiceProvider_compliance.cs | 4 ++-- src/Lamar/IServiceContext.cs | 5 ++++- src/Lamar/IoC/Instances/Instance.cs | 5 +++-- src/Lamar/ServiceGraph.cs | 15 ++++++++++++--- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Lamar.AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs b/src/Lamar.AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs index 9d5236f4..1f1cc170 100644 --- a/src/Lamar.AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs +++ b/src/Lamar.AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs @@ -9,6 +9,7 @@ namespace Lamar.AspNetCoreTests.Bugs; +#if NET8_0_OR_GREATER public class Bug_395_keyed_service_closed_generic_interface_registration_check { class ClassA {} @@ -45,4 +46,5 @@ public void do_not_blow_up() .ShouldNotBeNull(); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Lamar.Testing/IoC/Acceptance/IKeyedServiceProvider_compliance.cs b/src/Lamar.Testing/IoC/Acceptance/IKeyedServiceProvider_compliance.cs index be6cdaee..bfc28684 100644 --- a/src/Lamar.Testing/IoC/Acceptance/IKeyedServiceProvider_compliance.cs +++ b/src/Lamar.Testing/IoC/Acceptance/IKeyedServiceProvider_compliance.cs @@ -8,7 +8,7 @@ namespace Lamar.Testing.IoC.Acceptance; public class IKeyedServiceProvider_compliance { - #region sample_adding_keyed_services + #if NET8_0_OR_GREATER [Fact] public void register_by_name_using_dot_net_core_syntax() @@ -44,5 +44,5 @@ public void register_by_name_using_dot_net_core_syntax() .ShouldNotBeSameAs(container.GetKeyedService("C3")); } - #endregion + #endif } diff --git a/src/Lamar/IServiceContext.cs b/src/Lamar/IServiceContext.cs index 86dddcb1..a70ea3a9 100644 --- a/src/Lamar/IServiceContext.cs +++ b/src/Lamar/IServiceContext.cs @@ -8,7 +8,10 @@ namespace Lamar; -public interface IServiceContext : IServiceProvider, IDisposable, IAsyncDisposable, IKeyedServiceProvider +public interface IServiceContext : IServiceProvider, IDisposable, IAsyncDisposable +#if NET8_0_OR_GREATER + , IKeyedServiceProvider +#endif { /// /// Provides queryable access to the configured serviceType's and Instances of this Container. diff --git a/src/Lamar/IoC/Instances/Instance.cs b/src/Lamar/IoC/Instances/Instance.cs index c3f3c3e3..cb6d9398 100644 --- a/src/Lamar/IoC/Instances/Instance.cs +++ b/src/Lamar/IoC/Instances/Instance.cs @@ -106,10 +106,12 @@ internal IEnumerable ReferencedAssemblies() public static Instance For(ServiceDescriptor service) { + #if NET8_0_OR_GREATER if (service.IsKeyedService) { var name = service.ServiceKey?.ToString(); Instance instance = null; + if (service.KeyedImplementationInstance != null) { instance = new ObjectInstance(service.ServiceType, service.KeyedImplementationInstance); @@ -136,9 +138,8 @@ public static Instance For(ServiceDescriptor service) if (name.IsNotEmpty()) instance.Name = name; return instance; - - } + #endif if (service.ImplementationInstance is Instance i) diff --git a/src/Lamar/ServiceGraph.cs b/src/Lamar/ServiceGraph.cs index 8c47ed91..8d2c5b8d 100644 --- a/src/Lamar/ServiceGraph.cs +++ b/src/Lamar/ServiceGraph.cs @@ -268,9 +268,7 @@ private ServiceFamily buildFamilyForInstanceGroup(IServiceCollection services, private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollection services) { - var closed = services.Where(x => x.ServiceType == serviceType && (x.IsKeyedService - ? !x.KeyedImplementationType.IsOpenGeneric() - : !x.ImplementationType.IsOpenGeneric())) + var closed = services.Where(x => x.ServiceType == serviceType && isKeyedServiceSupported(x)) .Select(Instance.For); var templated = services @@ -297,6 +295,17 @@ private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollectio return new ServiceFamily(serviceType, DecoratorPolicies, instances); } + private static bool isKeyedServiceSupported(ServiceDescriptor serviceDescriptor) + { + #if NET8_0_OR_GREATER + return serviceDescriptor.IsKeyedService + ? !serviceDescriptor.KeyedImplementationType.IsOpenGeneric() + : !serviceDescriptor.ImplementationType.IsOpenGeneric(); + #endif + + return !serviceDescriptor.ImplementationType.IsOpenGeneric(); + } + public IEnumerable AllInstances() { var serviceFamilies = _families.Enumerate().Select(x => x.Value).Where(x => x != null).ToArray();