Skip to content

Using AddTypedClient throws "The HttpClient factory already has a registered client with the name" error #13346

@martyns

Description

@martyns

Edit by @rynowak See below for shiproom template

Describe the bug

Trying to register a Http Client service using AddTypedClient throws "The HttpClient factory already has a registered client with the name" on first request.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new ASP.NET Core Web Application -> v3.0 API project using preview 8 and Visual Studio 16.3.0 Preview 2.0
  2. Add the following in ConfigureServices
    services.AddHttpClient<IFoo>().AddTypedClient((hc, sp) => { return new Foo(); });
  3. Add the following class in the same namespace
public interface IFoo
{
}

public class Foo : IFoo
{
}
  1. Press F5

Expected behavior

The api is hit and result returned

Actual behaviour

The following error occurs:
The HttpClient factory already has a registered client with the name 'IFoo', bound to the type 'WebApplication2.IFoo'. Client names are computed based on the type name without considering the namespace ('IFoo'). Use an overload of AddHttpClient that accepts a string and provide a unique name to resolve the conflict.
at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.ReserveClient(IHttpClientBuilder builder, Type type, String name) at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTypedClient[TClient](IHttpClientBuilder builder, Func``3 factory) at WebApplication2.Startup.ConfigureServices(IServiceCollection services) in C:\Users\swanickm\source\repos\WebApplication2\WebApplication2\Startup.cs:line 29 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at WebApplication2.Program.Main(String[] args) in

Additional context

This works perfectly fine when using the ASP.NET Core 2.2 API template

Metadata

Metadata

Assignees

Labels

✔️ Resolution: FixedThe bug or enhancement requested in this issue has been checked-in!area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsbugThis issue describes a behavior which is not expected - a bug.feature-httpclientfactoryIncludes: HttpClientFactory (some bugs also in Extensions repo)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions