@@ -136,9 +136,9 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
136136 CancellationToken cancellationToken = default )
137137 {
138138 var concretions = new List < Type > ( ) ;
139- var interfaces = new List < Type > ( ) ;
139+ var interfaces = new HashSet < Type > ( ) ;
140140 var genericConcretions = new List < Type > ( ) ;
141- var genericInterfaces = new List < Type > ( ) ;
141+ var genericInterfaces = new HashSet < Type > ( ) ;
142142
143143 var types = assembliesToScan
144144 . SelectMany ( a => a . DefinedTypes )
@@ -154,55 +154,48 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
154154 if ( ! type . IsOpenGeneric ( ) )
155155 {
156156 concretions . Add ( type ) ;
157-
158- foreach ( var interfaceType in interfaceTypes )
159- {
160- interfaces . Fill ( interfaceType ) ;
161- }
157+ AddRangeDistinct ( interfaces , interfaceTypes ) ;
162158 }
163159 else
164160 {
165161 genericConcretions . Add ( type ) ;
166- foreach ( var interfaceType in interfaceTypes )
167- {
168- genericInterfaces . Fill ( interfaceType ) ;
169- }
162+ AddRangeDistinct ( genericInterfaces , interfaceTypes ) ;
170163 }
171164 }
172165
173- foreach ( var @interface in interfaces )
166+ foreach ( var iface in interfaces )
174167 {
175- var exactMatches = concretions . Where ( x => x . CanBeCastTo ( @interface ) ) . ToList ( ) ;
168+ var exactMatches = concretions . Where ( x => x . CanBeCastTo ( iface ) ) . ToList ( ) ;
176169 if ( addIfAlreadyExists )
177170 {
178171 foreach ( var type in exactMatches )
179172 {
180- services . AddTransient ( @interface , type ) ;
173+ services . AddTransient ( iface , type ) ;
181174 }
182175 }
183176 else
184177 {
185178 if ( exactMatches . Count > 1 )
186179 {
187- exactMatches . RemoveAll ( m => ! IsMatchingWithInterface ( m , @interface ) ) ;
180+ exactMatches . RemoveAll ( m => ! IsMatchingWithInterface ( m , iface ) ) ;
188181 }
189182
190183 foreach ( var type in exactMatches )
191184 {
192- services . TryAddTransient ( @interface , type ) ;
185+ services . TryAddTransient ( iface , type ) ;
193186 }
194187 }
195188
196- if ( ! @interface . IsOpenGeneric ( ) )
189+ if ( ! iface . IsOpenGeneric ( ) )
197190 {
198- AddConcretionsThatCouldBeClosed ( @interface , concretions , services ) ;
191+ AddConcretionsThatCouldBeClosed ( iface , concretions , services ) ;
199192 }
200193 }
201194
202- foreach ( var @interface in genericInterfaces )
195+ foreach ( var iface in genericInterfaces )
203196 {
204- var exactMatches = genericConcretions . Where ( x => x . CanBeCastTo ( @interface ) ) . ToList ( ) ;
205- AddAllConcretionsThatClose ( @interface , exactMatches , services , assembliesToScan , cancellationToken ) ;
197+ var exactMatches = genericConcretions . Where ( x => x . CanBeCastTo ( iface ) ) . ToList ( ) ;
198+ AddAllConcretionsThatClose ( iface , exactMatches , services , assembliesToScan , cancellationToken ) ;
206199 }
207200 }
208201
@@ -454,13 +447,21 @@ private static bool IsConcrete(this Type type)
454447 return ! type . IsAbstract && ! type . IsInterface ;
455448 }
456449
457- private static void Fill < T > ( this IList < T > list , T value )
450+ private static void AddRangeDistinct < T > ( ISet < T > set , IEnumerable < T > values )
458451 {
459- if ( list . Contains ( value ) )
452+ if ( set == null )
453+ {
454+ throw new ArgumentNullException ( nameof ( set ) ) ;
455+ }
456+ if ( values == null )
457+ {
458+ throw new ArgumentNullException ( nameof ( values ) ) ;
459+ }
460+
461+ foreach ( var value in values )
460462 {
461- return ;
463+ set . Add ( value ) ;
462464 }
463- list . Add ( value ) ;
464465 }
465466
466467 /// <summary>
@@ -498,13 +499,13 @@ public static void AddRequiredServices(IServiceCollection services, MediatorServ
498499 RegisterBehaviorIfImplementationsExist ( services , typeof ( RequestExceptionActionProcessorBehavior < , > ) , typeof ( IRequestExceptionAction < , > ) ) ;
499500 }
500501
501- if ( serviceConfiguration . RequestPreProcessorsToRegister . Any ( ) )
502+ if ( serviceConfiguration . RequestPreProcessorsToRegister . Count > 0 )
502503 {
503504 services . TryAddEnumerable ( new ServiceDescriptor ( typeof ( IPipelineBehavior < , > ) , typeof ( RequestPreProcessorBehavior < , > ) , ServiceLifetime . Transient ) ) ;
504505 services . TryAddEnumerable ( serviceConfiguration . RequestPreProcessorsToRegister ) ;
505506 }
506507
507- if ( serviceConfiguration . RequestPostProcessorsToRegister . Any ( ) )
508+ if ( serviceConfiguration . RequestPostProcessorsToRegister . Count > 0 )
508509 {
509510 services . TryAddEnumerable ( new ServiceDescriptor ( typeof ( IPipelineBehavior < , > ) , typeof ( RequestPostProcessorBehavior < , > ) , ServiceLifetime . Transient ) ) ;
510511 services . TryAddEnumerable ( serviceConfiguration . RequestPostProcessorsToRegister ) ;
0 commit comments