@@ -566,28 +566,13 @@ private static void ReserveClient(IHttpClientBuilder builder, Type type, string
566
566
var registry = ( HttpClientMappingRegistry ) builder . Services . Single ( sd => sd . ServiceType == typeof ( HttpClientMappingRegistry ) ) . ImplementationInstance ;
567
567
Debug . Assert ( registry != null ) ;
568
568
569
- // Check for same type registered twice. This can't work because typed clients have to be unique for DI to function.
570
- if ( registry . TypedClientRegistrations . TryGetValue ( type , out var otherName ) &&
571
-
572
- // Allow duplicate registrations with the same name. This is usually someone calling "AddHttpClient" once
573
- // as part of a library, and the consumer of that library doing the same thing to add further configuration.
574
- // See: https://github.com/aspnet/Extensions/issues/2077
575
- ! string . Equals ( name , otherName , StringComparison . Ordinal ) )
576
- {
577
- var message =
578
- $ "The HttpClient factory already has a registered client with the type '{ type . FullName } '. " +
579
- $ "Client types must be unique. " +
580
- $ "Consider using inheritance to create multiple unique types with the same API surface.";
581
- throw new InvalidOperationException ( message ) ;
582
- }
583
-
584
569
// Check for same name registered to two types. This won't work because we rely on named options for the configuration.
585
570
if ( registry . NamedClientRegistrations . TryGetValue ( name , out var otherType ) &&
586
571
587
572
// Allow using the same name with multiple types in some cases (see callers).
588
573
validateSingleType &&
589
574
590
- // Allow registering the same name twice to the same type (see above) .
575
+ // Allow registering the same name twice to the same type.
591
576
type != otherType )
592
577
{
593
578
var message =
@@ -597,8 +582,10 @@ private static void ReserveClient(IHttpClientBuilder builder, Type type, string
597
582
throw new InvalidOperationException ( message ) ;
598
583
}
599
584
600
- registry . TypedClientRegistrations [ type ] = name ;
601
- registry . NamedClientRegistrations [ name ] = type ;
585
+ if ( validateSingleType )
586
+ {
587
+ registry . NamedClientRegistrations [ name ] = type ;
588
+ }
602
589
}
603
590
}
604
591
}
0 commit comments