From e8425a060b3232f6957b39b6a2e66d7573a23dad Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Thu, 28 Sep 2023 11:21:29 -0500 Subject: [PATCH] hardening the code against a try find service family miss. Closes GH-340 --- .../Bug_340_open_generic_interface_registration_check.cs | 6 ++++-- src/Lamar/ServiceGraph.cs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Lamar.Testing/Bugs/Bug_340_open_generic_interface_registration_check.cs b/src/Lamar.Testing/Bugs/Bug_340_open_generic_interface_registration_check.cs index 518e11fe..f59e833d 100644 --- a/src/Lamar.Testing/Bugs/Bug_340_open_generic_interface_registration_check.cs +++ b/src/Lamar.Testing/Bugs/Bug_340_open_generic_interface_registration_check.cs @@ -12,7 +12,7 @@ interface IGenericService {} class ServiceA : IGenericService {} [Fact] - public void Test_it() + public void do_not_blow_up() { var container = new Container(x => { @@ -24,7 +24,9 @@ public void Test_it() container.GetInstance>() .ShouldNotBeNull(); + // This should return false. There's no registration for the + // open type, only a specific closed type container.Model.HasRegistrationFor(typeof(IGenericService<>)) - .ShouldBeTrue(); + .ShouldBeFalse(); } } \ No newline at end of file diff --git a/src/Lamar/ServiceGraph.cs b/src/Lamar/ServiceGraph.cs index 01222e3f..7f14dd7a 100644 --- a/src/Lamar/ServiceGraph.cs +++ b/src/Lamar/ServiceGraph.cs @@ -296,7 +296,8 @@ private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollectio public IEnumerable AllInstances() { - return _families.Enumerate().Select(x => x.Value).ToArray().SelectMany(x => x.All).ToArray(); + var serviceFamilies = _families.Enumerate().Select(x => x.Value).Where(x => x != null).ToArray(); + return serviceFamilies.SelectMany(x => x.All).ToArray(); } public bool HasFamily(Type serviceType)