One of the ways that you can inject an HttpClient instance in your ASP.NET Core application is through ‘Typed clients’. It is my preferred way as it takes maximal benefit from dependency injection, avoid the magic strings that ‘Named clients’ need and allows you to encapsulate the client configuration in one place.
A typical example of a named client is this:
This is possible thanks to following registration:
Most examples of the typed client you find out there are using a concrete class(like in the example above) but I wanted to use an interface.
Here was my first (naive) attempt:
The result of the code above was that an HttpClient instance was injected but it wasn’t using the configuration values I specified in the Startup.cs.
A second attempt was to specify the named client on the interface but this caused the same problem.
The correct solution is the following: