Skip to content

Commit

Permalink
Merge branch 'merge/release/3.1-to-master' of https://github.com/dotn…
Browse files Browse the repository at this point in the history
…et-maestro-bot/Common into dotnet-maestro-bot-merge/release/3.1-to-master
  • Loading branch information
Ryan Nowak committed Jan 16, 2020
2 parents 259a64a + 08a9b86 commit 3873a07
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 851 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</PropertyGroup>

<ItemGroup Condition=" '$(IsUnitTestProject)' == 'true' ">
<Reference Include="Microsoft.AspNetCore.Testing" />
<ProjectReference Include="$(MSBuildThisFileDirectory)src\TestingUtils\Microsoft.AspNetCore.Testing\src\Microsoft.AspNetCore.Testing.csproj" />
<Reference Include="Moq" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion eng/ProjectReferences.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<ProjectReferenceProvider Include="Microsoft.Extensions.Options.DataAnnotations" ProjectPath="$(RepoRoot)src\Options\DataAnnotations\src\Microsoft.Extensions.Options.DataAnnotations.csproj" RefProjectPath="$(RepoRoot)src\Options\DataAnnotations\ref\Microsoft.Extensions.Options.DataAnnotations.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Options" ProjectPath="$(RepoRoot)src\Options\Options\src\Microsoft.Extensions.Options.csproj" RefProjectPath="$(RepoRoot)src\Options\Options\ref\Microsoft.Extensions.Options.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Primitives" ProjectPath="$(RepoRoot)src\Primitives\src\Microsoft.Extensions.Primitives.csproj" RefProjectPath="$(RepoRoot)src\Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Testing" ProjectPath="$(RepoRoot)src\TestingUtils\Microsoft.AspNetCore.Testing\src\Microsoft.AspNetCore.Testing.csproj" RefProjectPath="$(RepoRoot)src\TestingUtils\Microsoft.AspNetCore.Testing\ref\Microsoft.AspNetCore.Testing.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.WebEncoders" ProjectPath="$(RepoRoot)src\WebEncoders\src\Microsoft.Extensions.WebEncoders.csproj" RefProjectPath="$(RepoRoot)src\WebEncoders\ref\Microsoft.Extensions.WebEncoders.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Testing" />
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
<Reference Include="Microsoft.Extensions.Logging" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder
throw new ArgumentNullException(nameof(builder));
}

ReserveClient(builder, typeof(TClient), builder.Name);
return AddTypedClientCore<TClient>(builder, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient>(this IHttpClientBuilder builder, bool validateSingleType)
where TClient : class
{
ReserveClient(builder, typeof(TClient), builder.Name, validateSingleType);

builder.Services.AddTransient<TClient>(s =>
{
Expand Down Expand Up @@ -378,7 +384,14 @@ public static IHttpClientBuilder AddTypedClient<TClient, TImplementation>(this I
throw new ArgumentNullException(nameof(builder));
}

ReserveClient(builder, typeof(TClient), builder.Name);
return AddTypedClientCore<TClient, TImplementation>(builder, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient, TImplementation>(this IHttpClientBuilder builder, bool validateSingleType)
where TClient : class
where TImplementation : class, TClient
{
ReserveClient(builder, typeof(TClient), builder.Name, validateSingleType);

builder.Services.AddTransient<TClient>(s =>
{
Expand Down Expand Up @@ -426,7 +439,13 @@ public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder
throw new ArgumentNullException(nameof(factory));
}

ReserveClient(builder, typeof(TClient), builder.Name);
return AddTypedClientCore<TClient>(builder, factory, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient>(this IHttpClientBuilder builder, Func<HttpClient, TClient> factory, bool validateSingleType)
where TClient : class
{
ReserveClient(builder, typeof(TClient), builder.Name, validateSingleType);

builder.Services.AddTransient<TClient>(s =>
{
Expand Down Expand Up @@ -473,7 +492,23 @@ public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder
throw new ArgumentNullException(nameof(factory));
}

ReserveClient(builder, typeof(TClient), builder.Name);
return AddTypedClientCore<TClient>(builder, factory, validateSingleType: false);
}

internal static IHttpClientBuilder AddTypedClientCore<TClient>(this IHttpClientBuilder builder, Func<HttpClient, IServiceProvider, TClient> factory, bool validateSingleType)
where TClient : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}

ReserveClient(builder, typeof(TClient), builder.Name, validateSingleType);

builder.Services.AddTransient<TClient>(s =>
{
Expand Down Expand Up @@ -583,23 +618,19 @@ public static IHttpClientBuilder SetHandlerLifetime(this IHttpClientBuilder buil
}

// See comments on HttpClientMappingRegistry.
private static void ReserveClient(IHttpClientBuilder builder, Type type, string name)
private static void ReserveClient(IHttpClientBuilder builder, Type type, string name, bool validateSingleType)
{
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))
{
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))
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.
type != otherType)
{
var message =
$"The HttpClient factory already has a registered client with the name '{name}', bound to the type '{otherType.FullName}'. " +
Expand All @@ -608,8 +639,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 @@ -210,7 +210,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection

var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS

var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -298,7 +298,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
AddHttpClient(services);

var builder = new DefaultHttpClientBuilder(services, name);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: false); // Name was explicitly provided.
return builder;
}

Expand Down Expand Up @@ -349,7 +349,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
AddHttpClient(services);

var builder = new DefaultHttpClientBuilder(services, name);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: false); // name was explicitly provided
return builder;
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -439,7 +439,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -489,7 +489,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -539,7 +539,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: true);
return builder;
}

Expand Down Expand Up @@ -591,7 +591,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection

var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: false); // name was explicitly provided
return builder;
}

Expand Down Expand Up @@ -643,7 +643,7 @@ public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection

var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient>();
builder.AddTypedClientCore<TClient>(validateSingleType: false); // name was explictly provided
return builder;
}

Expand Down Expand Up @@ -700,7 +700,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS

var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: false); // name was explicitly provided
return builder;
}

Expand Down Expand Up @@ -757,7 +757,7 @@ public static IHttpClientBuilder AddHttpClient<TClient, TImplementation>(this IS

var builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClient<TClient, TImplementation>();
builder.AddTypedClientCore<TClient, TImplementation>(validateSingleType: false); // name was explicitly provided
return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.Extensions.DependencyInjection
// See: https://github.com/dotnet/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>();
}
}
Loading

0 comments on commit 3873a07

Please sign in to comment.