Skip to content

Commit

Permalink
Use 'keyed service' code only for .net 8 and newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
rizi authored and jeremydmiller committed Nov 18, 2024
1 parent 39676a3 commit 96e2fe0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -45,4 +46,5 @@ public void do_not_blow_up()
.ShouldNotBeNull();
}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -44,5 +44,5 @@ public void register_by_name_using_dot_net_core_syntax()
.ShouldNotBeSameAs(container.GetKeyedService<CWidget>("C3"));
}

#endregion
#endif
}
5 changes: 4 additions & 1 deletion src/Lamar/IServiceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Provides queryable access to the configured serviceType's and Instances of this Container.
Expand Down
5 changes: 3 additions & 2 deletions src/Lamar/IoC/Instances/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ internal IEnumerable<Assembly> 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);
Expand All @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions src/Lamar/ServiceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Instance> AllInstances()
{
var serviceFamilies = _families.Enumerate().Select(x => x.Value).Where(x => x != null).ToArray();
Expand Down

0 comments on commit 96e2fe0

Please sign in to comment.