Skip to content

Commit 9b83c2e

Browse files
committed
All duplicates of the same type with different names
Also reported as part of dotnet/extensions#2077 In this pattern users register many instances of the same client with different configurations, and multiplex between them in a round-robin fashion. \n\nCommit migrated from dotnet/extensions@edcf5f2
1 parent 99c677b commit 9b83c2e

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/HttpClientFactory/Http/test/DependencyInjection/HttpClientFactoryServiceCollectionExtensionsTest.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Net;
89
using System.Net.Http;
910
using System.Threading;
@@ -392,39 +393,37 @@ public void AddHttpClient_AddSameTypedClientTwice_WithSameName_WithAddTypedClien
392393
}
393394

394395
[Fact]
395-
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_ThrowsError()
396+
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_IsAllowed()
396397
{
397398
// Arrange
398399
var serviceCollection = new ServiceCollection();
399-
serviceCollection.AddHttpClient<TestTypedClient>();
400+
serviceCollection.AddHttpClient<TestTypedClient>("Test1");
401+
serviceCollection.AddHttpClient<TestTypedClient>("Test2");
402+
403+
var services = serviceCollection.BuildServiceProvider();
400404

401405
// Act
402-
var ex = Assert.Throws<InvalidOperationException>(() => serviceCollection.AddHttpClient<TestTypedClient>("Test"));
406+
var clients = services.GetRequiredService<IEnumerable<TestTypedClient>>();
403407

404408
// Assert
405-
Assert.Equal(
406-
"The HttpClient factory already has a registered client with the type 'Microsoft.Extensions.Http.TestTypedClient'. " +
407-
"Client types must be unique. " +
408-
"Consider using inheritance to create multiple unique types with the same API surface.",
409-
ex.Message);
409+
Assert.Equal(2, clients.Count());
410410
}
411411

412412
[Fact]
413-
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_WithAddTypedClient_ThrowsError()
413+
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_WithAddTypedClient_IsAllowed()
414414
{
415415
// Arrange
416416
var serviceCollection = new ServiceCollection();
417417
serviceCollection.AddHttpClient<TestTypedClient>();
418+
serviceCollection.AddHttpClient("Test").AddTypedClient<TestTypedClient>();
419+
420+
var services = serviceCollection.BuildServiceProvider();
418421

419422
// Act
420-
var ex = Assert.Throws<InvalidOperationException>(() => serviceCollection.AddHttpClient("Test").AddTypedClient<TestTypedClient>());
423+
var clients = services.GetRequiredService<IEnumerable<TestTypedClient>>();
421424

422425
// Assert
423-
Assert.Equal(
424-
"The HttpClient factory already has a registered client with the type 'Microsoft.Extensions.Http.TestTypedClient'. " +
425-
"Client types must be unique. " +
426-
"Consider using inheritance to create multiple unique types with the same API surface.",
427-
ex.Message);
426+
Assert.Equal(2, clients.Count());
428427
}
429428

430429
[Fact]

0 commit comments

Comments
 (0)