Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rynowak committed Jan 16, 2020
1 parent f198c46 commit 9c9ac59
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -566,28 +566,13 @@ private static void ReserveClient(IHttpClientBuilder builder, Type type, string
var registry = (HttpClientMappingRegistry)builder.Services.Single(sd => sd.ServiceType == typeof(HttpClientMappingRegistry)).ImplementationInstance;
Debug.Assert(registry != null);

// Check for same type registered twice. This can't work because typed clients have to be unique for DI to function.
if (registry.TypedClientRegistrations.TryGetValue(type, out var otherName) &&

// Allow duplicate registrations with the same name. This is usually someone calling "AddHttpClient" once
// as part of a library, and the consumer of that library doing the same thing to add further configuration.
// See: https://github.com/aspnet/Extensions/issues/2077
!string.Equals(name, otherName, StringComparison.Ordinal))
{
var message =
$"The HttpClient factory already has a registered client with the type '{type.FullName}'. " +
$"Client types must be unique. " +
$"Consider using inheritance to create multiple unique types with the same API surface.";
throw new InvalidOperationException(message);
}

// Check for same name registered to two types. This won't work because we rely on named options for the configuration.
if (registry.NamedClientRegistrations.TryGetValue(name, out var otherType) &&

// Allow using the same name with multiple types in some cases (see callers).
validateSingleType &&

// Allow registering the same name twice to the same type (see above).
// Allow registering the same name twice to the same type.
type != otherType)
{
var message =
Expand All @@ -597,8 +582,10 @@ private static void ReserveClient(IHttpClientBuilder builder, Type type, string
throw new InvalidOperationException(message);
}

registry.TypedClientRegistrations[type] = name;
registry.NamedClientRegistrations[name] = type;
if (validateSingleType)
{
registry.NamedClientRegistrations[name] = type;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.Extensions.DependencyInjection
// See: https://github.com/aspnet/Extensions/issues/960
internal class HttpClientMappingRegistry
{
public Dictionary<Type, string> TypedClientRegistrations { get; } = new Dictionary<Type, string>();

public Dictionary<string, Type> NamedClientRegistrations { get; } = new Dictionary<string, Type>();
}
}

0 comments on commit 9c9ac59

Please sign in to comment.