Skip to content

Commit

Permalink
All duplicates of the same type with different names
Browse files Browse the repository at this point in the history
Also reported as part of #2077

In this pattern users register many instances of the same client with
different configurations, and multiplex between them in a round-robin
fashion.
  • Loading branch information
rynowak committed Jan 16, 2020
1 parent 9c9ac59 commit edcf5f2
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -392,39 +393,37 @@ public void AddHttpClient_AddSameTypedClientTwice_WithSameName_WithAddTypedClien
}

[Fact]
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_ThrowsError()
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_IsAllowed()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient<TestTypedClient>();
serviceCollection.AddHttpClient<TestTypedClient>("Test1");
serviceCollection.AddHttpClient<TestTypedClient>("Test2");

var services = serviceCollection.BuildServiceProvider();

// Act
var ex = Assert.Throws<InvalidOperationException>(() => serviceCollection.AddHttpClient<TestTypedClient>("Test"));
var clients = services.GetRequiredService<IEnumerable<TestTypedClient>>();

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

[Fact]
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_WithAddTypedClient_ThrowsError()
public void AddHttpClient_AddSameTypedClientTwice_WithDifferentNames_WithAddTypedClient_IsAllowed()
{
// Arrange
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient<TestTypedClient>();
serviceCollection.AddHttpClient("Test").AddTypedClient<TestTypedClient>();

var services = serviceCollection.BuildServiceProvider();

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

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

[Fact]
Expand Down

0 comments on commit edcf5f2

Please sign in to comment.