Skip to content

Commit c3a9726

Browse files
committed
Exclude AnyKey services from showing up in collection resolutions.
1 parent 1f379f2 commit c3a9726

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Autofac.Extensions.DependencyInjection/AutofacRegistration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Autofac.Core.Activators;
77
using Autofac.Core.Activators.Delegate;
88
using Autofac.Core.Activators.Reflection;
9+
using Autofac.Core.Registration;
910
using Autofac.Core.Resolving.Pipeline;
1011
using Microsoft.Extensions.DependencyInjection;
1112

@@ -191,6 +192,11 @@ private static IRegistrationBuilder<object, TActivatorData, TRegistrationStyle>
191192

192193
// If it's keyed, the service key won't be null. A null key results in it _not_ being a keyed service.
193194
registrationBuilder.Keyed(key, descriptor.ServiceType);
195+
if (descriptor.ServiceKey is not null && descriptor.ServiceKey.Equals(Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey))
196+
{
197+
// Exclude AnyKey registrations from collection resolutions.
198+
registrationBuilder.RegistrationData.Options |= RegistrationOptions.ExcludeFromCollections;
199+
}
194200
}
195201
else
196202
{

0 commit comments

Comments
 (0)