@@ -29,6 +29,40 @@ public static IEnumerable<Parameter> AddKeyedServiceParameter(Service service, I
2929 return AddKeyedServiceParameter ( keyedService . ServiceKey , parameters ) ;
3030 }
3131
32+ /// <summary>
33+ /// Ensures keyed service requests carry their associated key with the parameter sequence.
34+ /// </summary>
35+ /// <param name="service">The service being resolved.</param>
36+ /// <param name="parameters">The parameters supplied by the caller.</param>
37+ /// <param name="registration">The component registration (if known).</param>
38+ /// <returns>An enumerable that exposes the keyed service key when appropriate.</returns>
39+ public static IEnumerable < Parameter > AddKeyedServiceParameter ( Service service , IEnumerable < Parameter > parameters , IComponentRegistration ? registration )
40+ {
41+ if ( service == null )
42+ {
43+ throw new ArgumentNullException ( nameof ( service ) ) ;
44+ }
45+
46+ if ( parameters == null )
47+ {
48+ throw new ArgumentNullException ( nameof ( parameters ) ) ;
49+ }
50+
51+ if ( service is not KeyedService keyedService || KeyedService . IsAnyKey ( keyedService . ServiceKey ) )
52+ {
53+ // It's not a keyed service OR it's registered with AnyKey.
54+ return parameters ;
55+ }
56+
57+ if ( registration ? . Activator is Autofac . Core . Activators . Reflection . ReflectionActivator reflectionActivator &&
58+ ! reflectionActivator . RequiresServiceKeyParameter )
59+ {
60+ return parameters ;
61+ }
62+
63+ return AddKeyedServiceParameter ( keyedService . ServiceKey , parameters ) ;
64+ }
65+
3266 /// <summary>
3367 /// Ensures keyed service requests carry their associated key with the parameter sequence.
3468 /// </summary>
@@ -47,7 +81,12 @@ public static IEnumerable<Parameter> AddKeyedServiceParameter(object serviceKey,
4781 throw new ArgumentNullException ( nameof ( parameters ) ) ;
4882 }
4983
50- if ( KeyedService . IsAnyKey ( serviceKey ) || HasKeyParameter ( parameters , serviceKey ) )
84+ if ( KeyedService . IsAnyKey ( serviceKey ) )
85+ {
86+ return parameters ;
87+ }
88+
89+ if ( HasKeyParameter ( parameters , serviceKey ) )
5190 {
5291 return parameters ;
5392 }
@@ -56,7 +95,17 @@ public static IEnumerable<Parameter> AddKeyedServiceParameter(object serviceKey,
5695 }
5796
5897 private static bool HasKeyParameter ( IEnumerable < Parameter > parameters , object serviceKey )
59- => parameters . OfType < KeyedServiceKeyParameter > ( ) . Any ( p => Equals ( p . ServiceKey , serviceKey ) ) ;
98+ {
99+ foreach ( var parameter in parameters )
100+ {
101+ if ( parameter is KeyedServiceKeyParameter keyParameter && Equals ( keyParameter . ServiceKey , serviceKey ) )
102+ {
103+ return true ;
104+ }
105+ }
106+
107+ return false ;
108+ }
60109
61110 private static IEnumerable < Parameter > AppendKeyParameter ( IEnumerable < Parameter > parameters , object serviceKey )
62111 {
0 commit comments