Skip to content

Commit 96e2fe0

Browse files
rizijeremydmiller
authored andcommitted
Use 'keyed service' code only for .net 8 and newer versions
1 parent 39676a3 commit 96e2fe0

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

src/Lamar.AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Lamar.AspNetCoreTests.Bugs;
1111

12+
#if NET8_0_OR_GREATER
1213
public class Bug_395_keyed_service_closed_generic_interface_registration_check
1314
{
1415
class ClassA {}
@@ -45,4 +46,5 @@ public void do_not_blow_up()
4546
.ShouldNotBeNull();
4647
}
4748
}
48-
}
49+
}
50+
#endif

src/Lamar.Testing/IoC/Acceptance/IKeyedServiceProvider_compliance.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Lamar.Testing.IoC.Acceptance;
88

99
public class IKeyedServiceProvider_compliance
1010
{
11-
#region sample_adding_keyed_services
11+
#if NET8_0_OR_GREATER
1212

1313
[Fact]
1414
public void register_by_name_using_dot_net_core_syntax()
@@ -44,5 +44,5 @@ public void register_by_name_using_dot_net_core_syntax()
4444
.ShouldNotBeSameAs(container.GetKeyedService<CWidget>("C3"));
4545
}
4646

47-
#endregion
47+
#endif
4848
}

src/Lamar/IServiceContext.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
namespace Lamar;
1010

11-
public interface IServiceContext : IServiceProvider, IDisposable, IAsyncDisposable, IKeyedServiceProvider
11+
public interface IServiceContext : IServiceProvider, IDisposable, IAsyncDisposable
12+
#if NET8_0_OR_GREATER
13+
, IKeyedServiceProvider
14+
#endif
1215
{
1316
/// <summary>
1417
/// Provides queryable access to the configured serviceType's and Instances of this Container.

src/Lamar/IoC/Instances/Instance.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ internal IEnumerable<Assembly> ReferencedAssemblies()
106106

107107
public static Instance For(ServiceDescriptor service)
108108
{
109+
#if NET8_0_OR_GREATER
109110
if (service.IsKeyedService)
110111
{
111112
var name = service.ServiceKey?.ToString();
112113
Instance instance = null;
114+
113115
if (service.KeyedImplementationInstance != null)
114116
{
115117
instance = new ObjectInstance(service.ServiceType, service.KeyedImplementationInstance);
@@ -136,9 +138,8 @@ public static Instance For(ServiceDescriptor service)
136138
if (name.IsNotEmpty()) instance.Name = name;
137139

138140
return instance;
139-
140-
141141
}
142+
#endif
142143

143144

144145
if (service.ImplementationInstance is Instance i)

src/Lamar/ServiceGraph.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ private ServiceFamily buildFamilyForInstanceGroup(IServiceCollection services,
268268

269269
private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollection services)
270270
{
271-
var closed = services.Where(x => x.ServiceType == serviceType && (x.IsKeyedService
272-
? !x.KeyedImplementationType.IsOpenGeneric()
273-
: !x.ImplementationType.IsOpenGeneric()))
271+
var closed = services.Where(x => x.ServiceType == serviceType && isKeyedServiceSupported(x))
274272
.Select(Instance.For);
275273

276274
var templated = services
@@ -297,6 +295,17 @@ private ServiceFamily buildClosedGenericType(Type serviceType, IServiceCollectio
297295
return new ServiceFamily(serviceType, DecoratorPolicies, instances);
298296
}
299297

298+
private static bool isKeyedServiceSupported(ServiceDescriptor serviceDescriptor)
299+
{
300+
#if NET8_0_OR_GREATER
301+
return serviceDescriptor.IsKeyedService
302+
? !serviceDescriptor.KeyedImplementationType.IsOpenGeneric()
303+
: !serviceDescriptor.ImplementationType.IsOpenGeneric();
304+
#endif
305+
306+
return !serviceDescriptor.ImplementationType.IsOpenGeneric();
307+
}
308+
300309
public IEnumerable<Instance> AllInstances()
301310
{
302311
var serviceFamilies = _families.Enumerate().Select(x => x.Value).Where(x => x != null).ToArray();

0 commit comments

Comments
 (0)