@@ -28,7 +28,7 @@ public void AddHttpClient_IsSelfContained_CanCreateClient()
28
28
var serviceCollection = new ServiceCollection ( ) ;
29
29
30
30
// Act1
31
- serviceCollection . AddHttpClient ( ) ;
31
+ serviceCollection . AddHttpClient ( ) ;
32
32
33
33
var services = serviceCollection . BuildServiceProvider ( ) ;
34
34
var options = services . GetRequiredService < IOptionsMonitor < HttpClientFactoryOptions > > ( ) ;
@@ -446,21 +446,88 @@ public void AddHttpClient_AddSameNameWithTypedClientTwice_ThrowsError()
446
446
}
447
447
448
448
[ Fact ]
449
- public void AddHttpClient_AddSameNameWithTypedClientTwice_WithAddTypedClient_ThrowsError ( )
449
+ public void AddHttpClient_AddSameNameWithTypedClientTwice_WithAddTypedClient_IsAllowed ( )
450
450
{
451
451
// Arrange
452
452
var serviceCollection = new ServiceCollection ( ) ;
453
- serviceCollection . AddHttpClient < TestTypedClient > ( ) ;
453
+ serviceCollection . AddHttpClient < TestTypedClient > ( c =>
454
+ {
455
+ c . BaseAddress = new Uri ( "http://example.com" ) ;
456
+ } ) ;
454
457
455
458
// Act
456
- var ex = Assert . Throws < InvalidOperationException > ( ( ) => serviceCollection . AddHttpClient ( "TestTypedClient" ) . AddTypedClient < AnotherNamespace . TestTypedClient > ( ) ) ;
459
+ serviceCollection . AddHttpClient ( "TestTypedClient" ) . AddTypedClient < AnotherNamespace . TestTypedClient > ( ) ;
460
+
461
+ var services = serviceCollection . BuildServiceProvider ( ) ;
462
+
463
+ // Act
464
+ var client = services . GetRequiredService < TestTypedClient > ( ) ;
457
465
458
466
// Assert
459
- Assert . Equal (
460
- "The HttpClient factory already has a registered client with the name 'TestTypedClient', bound to the type 'Microsoft.Extensions.Http.TestTypedClient'. " +
461
- "Client names are computed based on the type name without considering the namespace ('TestTypedClient'). " +
462
- "Use an overload of AddHttpClient that accepts a string and provide a unique name to resolve the conflict." ,
463
- ex . Message ) ;
467
+ Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
468
+
469
+ // Act
470
+ var client2 = services . GetRequiredService < AnotherNamespace . TestTypedClient > ( ) ;
471
+
472
+ // Assert
473
+ Assert . Equal ( "http://example.com/" , client2 . HttpClient . BaseAddress . AbsoluteUri ) ;
474
+ }
475
+
476
+ [ Fact ]
477
+ public void AddHttpClient_AddSameNameWithTypedClientTwice_WithExplicitName_IsAllowed ( )
478
+ {
479
+ // Arrange
480
+ var serviceCollection = new ServiceCollection ( ) ;
481
+ serviceCollection . AddHttpClient < TestTypedClient > ( c =>
482
+ {
483
+ c . BaseAddress = new Uri ( "http://example.com" ) ;
484
+ } ) ;
485
+
486
+ // Act
487
+ serviceCollection . AddHttpClient < AnotherNamespace . TestTypedClient > ( "TestTypedClient" ) ;
488
+
489
+ var services = serviceCollection . BuildServiceProvider ( ) ;
490
+
491
+ // Act
492
+ var client = services . GetRequiredService < TestTypedClient > ( ) ;
493
+
494
+ // Assert
495
+ Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
496
+
497
+ // Act
498
+ var client2 = services . GetRequiredService < AnotherNamespace . TestTypedClient > ( ) ;
499
+
500
+ // Assert
501
+ Assert . Equal ( "http://example.com/" , client2 . HttpClient . BaseAddress . AbsoluteUri ) ;
502
+ }
503
+
504
+ [ Fact ]
505
+ public void AddHttpClient_RegisteringMultipleTypes_WithAddTypedClient_IsAllowed ( )
506
+ {
507
+ // Arrange
508
+ var serviceCollection = new ServiceCollection ( ) ;
509
+
510
+ // Act
511
+ serviceCollection . AddHttpClient ( "Test" , c =>
512
+ {
513
+ c . BaseAddress = new Uri ( "http://example.com" ) ;
514
+ } )
515
+ . AddTypedClient < TestTypedClient > ( )
516
+ . AddTypedClient < AnotherNamespace . TestTypedClient > ( ) ;
517
+
518
+ var services = serviceCollection . BuildServiceProvider ( ) ;
519
+
520
+ // Act
521
+ var client = services . GetRequiredService < TestTypedClient > ( ) ;
522
+
523
+ // Assert
524
+ Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
525
+
526
+ // Act
527
+ var client2 = services . GetRequiredService < AnotherNamespace . TestTypedClient > ( ) ;
528
+
529
+ // Assert
530
+ Assert . Equal ( "http://example.com/" , client2 . HttpClient . BaseAddress . AbsoluteUri ) ;
464
531
}
465
532
466
533
[ Fact ]
@@ -475,7 +542,7 @@ public void AddHttpClient_AddTypedClient_WithServiceDelegate_ConfiguresNamedClie
475
542
} ) ;
476
543
477
544
// Act
478
- serviceCollection . AddHttpClient ( "test" ) . AddTypedClient < TestTypedClient > ( ( c , s ) =>
545
+ serviceCollection . AddHttpClient ( "test" ) . AddTypedClient < TestTypedClient > ( ( c , s ) =>
479
546
{
480
547
Assert . Equal ( "http://example.com/" , c . BaseAddress . AbsoluteUri ) ;
481
548
c . BaseAddress = new Uri ( "http://example2.com" ) ;
@@ -564,7 +631,7 @@ public void AddHttpClient_WithTypedClient_AndServiceDelegate_ConfiguresClient()
564
631
} ) ;
565
632
566
633
// Act1
567
- serviceCollection . AddHttpClient < TestTypedClient > ( ( s , c ) =>
634
+ serviceCollection . AddHttpClient < TestTypedClient > ( ( s , c ) =>
568
635
{
569
636
var options = s . GetRequiredService < IOptions < OtherTestOptions > > ( ) ;
570
637
c . BaseAddress = new Uri ( options . Value . BaseAddress ) ;
@@ -574,7 +641,7 @@ public void AddHttpClient_WithTypedClient_AndServiceDelegate_ConfiguresClient()
574
641
575
642
// Act2
576
643
var client = services . GetRequiredService < TestTypedClient > ( ) ;
577
-
644
+
578
645
// Assert
579
646
Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
580
647
}
@@ -591,7 +658,7 @@ public void AddHttpClient_WithTypedClientAndImplementation_AndServiceDelegate_Co
591
658
} ) ;
592
659
593
660
// Act1
594
- serviceCollection . AddHttpClient < ITestTypedClient , TestTypedClient > ( ( s , c ) =>
661
+ serviceCollection . AddHttpClient < ITestTypedClient , TestTypedClient > ( ( s , c ) =>
595
662
{
596
663
var options = s . GetRequiredService < IOptions < OtherTestOptions > > ( ) ;
597
664
c . BaseAddress = new Uri ( options . Value . BaseAddress ) ;
@@ -601,7 +668,7 @@ public void AddHttpClient_WithTypedClientAndImplementation_AndServiceDelegate_Co
601
668
602
669
// Act2
603
670
var client = services . GetRequiredService < ITestTypedClient > ( ) ;
604
-
671
+
605
672
// Assert
606
673
Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
607
674
}
@@ -618,7 +685,7 @@ public void AddHttpClient_WithTypedClient_AndServiceDelegate_ConfiguresNamedClie
618
685
} ) ;
619
686
620
687
// Act1
621
- serviceCollection . AddHttpClient < TestTypedClient > ( "test" , ( s , c ) =>
688
+ serviceCollection . AddHttpClient < TestTypedClient > ( "test" , ( s , c ) =>
622
689
{
623
690
var options = s . GetRequiredService < IOptions < OtherTestOptions > > ( ) ;
624
691
c . BaseAddress = new Uri ( options . Value . BaseAddress ) ;
@@ -628,7 +695,7 @@ public void AddHttpClient_WithTypedClient_AndServiceDelegate_ConfiguresNamedClie
628
695
629
696
// Act2
630
697
var client = services . GetRequiredService < TestTypedClient > ( ) ;
631
-
698
+
632
699
// Assert
633
700
Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
634
701
}
@@ -645,7 +712,7 @@ public void AddHttpClient_WithTypedClientAndImplementation_AndServiceDelegate_Co
645
712
} ) ;
646
713
647
714
// Act1
648
- serviceCollection . AddHttpClient < ITestTypedClient , TestTypedClient > ( "test" , ( s , c ) =>
715
+ serviceCollection . AddHttpClient < ITestTypedClient , TestTypedClient > ( "test" , ( s , c ) =>
649
716
{
650
717
var options = s . GetRequiredService < IOptions < OtherTestOptions > > ( ) ;
651
718
c . BaseAddress = new Uri ( options . Value . BaseAddress ) ;
@@ -655,7 +722,7 @@ public void AddHttpClient_WithTypedClientAndImplementation_AndServiceDelegate_Co
655
722
656
723
// Act2
657
724
var client = services . GetRequiredService < ITestTypedClient > ( ) ;
658
-
725
+
659
726
// Assert
660
727
Assert . Equal ( "http://example.com/" , client . HttpClient . BaseAddress . AbsoluteUri ) ;
661
728
}
@@ -1171,6 +1238,9 @@ public class TestTypedClient
1171
1238
{
1172
1239
public TestTypedClient ( HttpClient httpClient )
1173
1240
{
1241
+ HttpClient = httpClient ;
1174
1242
}
1243
+
1244
+ public HttpClient HttpClient { get ; }
1175
1245
}
1176
1246
}
0 commit comments