From a9fd707b6531984049c7b31a213e65330cb233f9 Mon Sep 17 00:00:00 2001 From: rizi Date: Thu, 14 Nov 2024 22:38:39 +0100 Subject: [PATCH] Fix IEnumerable can't be resolved during startup. --- ...ServiceProviderIsService_implementation.cs | 48 ++++++++++++++++++- src/Lamar/ServiceGraph.cs | 4 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Lamar.Testing/IoC/Acceptance/IServiceProviderIsService_implementation.cs b/src/Lamar.Testing/IoC/Acceptance/IServiceProviderIsService_implementation.cs index ae7559b5..947b3022 100644 --- a/src/Lamar.Testing/IoC/Acceptance/IServiceProviderIsService_implementation.cs +++ b/src/Lamar.Testing/IoC/Acceptance/IServiceProviderIsService_implementation.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; + using Shouldly; using StructureMap.Testing.Widget; using Xunit; @@ -42,7 +44,7 @@ public void fix_for_391_dont_resolve_collections_with_not_registered_type() { var containerWithNoRegistrations = Container.Empty(); - containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeFalse(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); containerWithNoRegistrations.IsService(typeof(IReadOnlyCollection)).ShouldBeFalse(); containerWithNoRegistrations.IsService(typeof(IList)).ShouldBeFalse(); containerWithNoRegistrations.IsService(typeof(List)).ShouldBeFalse(); @@ -58,6 +60,50 @@ public void fix_for_391_dont_resolve_collections_with_not_registered_type() containerWithRegistrations.IsService(typeof(List)).ShouldBeTrue(); } + [Fact] + public void fix_for_405_add_support_for_net9() + { + var containerWithNoRegistrations = Container.Empty(); + + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithNoRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + + containerWithNoRegistrations.IsService(typeof(IReadOnlyCollection)).ShouldBeFalse(); + containerWithNoRegistrations.IsService(typeof(IReadOnlyList)).ShouldBeFalse(); + containerWithNoRegistrations.IsService(typeof(IList)).ShouldBeFalse(); + containerWithNoRegistrations.IsService(typeof(List)).ShouldBeFalse(); + + List integerList = new List(); + + var containerWithRegistrations = Container.For(services => + { + services.ForConcreteType(); + services.For>().Use(integerList); + }); + + containerWithRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + containerWithRegistrations.IsService(typeof(IReadOnlyCollection)).ShouldBeTrue(); + containerWithRegistrations.IsService(typeof(IReadOnlyList)).ShouldBeTrue(); + containerWithRegistrations.IsService(typeof(IList)).ShouldBeTrue(); + containerWithRegistrations.IsService(typeof(List)).ShouldBeTrue(); + + containerWithRegistrations.IsService(typeof(IEnumerable)).ShouldBeTrue(); + + containerWithRegistrations.GetInstance>().ShouldBeSameAs(integerList); + } + public interface IService { } diff --git a/src/Lamar/ServiceGraph.cs b/src/Lamar/ServiceGraph.cs index f2d311c4..8c47ed91 100644 --- a/src/Lamar/ServiceGraph.cs +++ b/src/Lamar/ServiceGraph.cs @@ -498,7 +498,7 @@ internal ServiceFamily TryToCreateMissingFamilyWithNetCoreRules(Type serviceType private Type getServiceTypeThatTakesCollectionsIntoAccount(Type serviceType) { - if (!typeof(IEnumerable).IsAssignableFrom(serviceType) || serviceType.GetGenericArguments().Length != 1) + if (!typeof(IEnumerable).IsAssignableFrom(serviceType) || serviceType.GetGenericArguments().Length != 1 || serviceType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) return serviceType; Type type = serviceType.GetGenericArguments().Single(); @@ -507,7 +507,7 @@ private Type getServiceTypeThatTakesCollectionsIntoAccount(Type serviceType) return isTypeRegistered ? serviceType : type; } - + internal void ClearPlanning() { _chain.Clear();