4

I'm trying to register a Typed HttpClient in my ASP.NET 2.1 Web Application where the Typed HttpClient requires an API Key, defined in the constructor.

e.g.

Startup.cs
----------
services.AddHttpClient<MyHttpClient>();


MyHttpClient.cs
---------------
public class MyHttpClient(string apiKey, HttpClient httpClient) { .. }

As you can see, I'm pretty sure the IoC/DI wouldn't know how to create the instance of the MyHttpClient because it wouldn't know what value to inject into the string apiKey parameter.

Is this possible?

I've also tried referencing the Microsoft docs about "Fundamentals - Http Requests (Typed Clients)" but couldn't see any examples of this.

4
  • 1
    Could apiKey instead be IApiKeyProvider? Then build ApiKeyProvider which provides the apiKey. Commented Jun 8, 2018 at 4:34
  • You can configure your api key in Startup when you register the typed client services.AddHttpClient<MyHttpClient>(c => { c.DefaultRequestHeaders.Add("Authorization", "{apikey}"); }); Commented Jun 8, 2018 at 4:45
  • @Brad great - worked :) create an answer and u can score some points, buddy :) Commented Jun 8, 2018 at 5:29
  • Does your MyHttpClient actually derive from HttpClient or is it a wrapper? Commented Jun 8, 2018 at 5:39

2 Answers 2

4

You can configure your api key in Startup when you register the typed client

services.AddHttpClient<MyHttpClient>(c => 
{ 
    c.DefaultRequestHeaders.Add("Authorization", "{apikey}"); 
});
Sign up to request clarification or add additional context in comments.

2 Comments

Is this with .net core 2.1 ? I didn't find the AddHttpClient in IServiceCollection
@SanJaisy It's whatever version .net core was at 3 years ago. The current version still has the same extension methods for adding typed clients. Make sure you have using Microsoft.Extensions.DependencyInjection in your startup file.
4

For named clients the answer of Brad may be the only solution. But for typed clients you can inject the key as suggested by mjwills.

In startup:

services.AddSingleton<IApiKeyProvider, ApiKeyProvider>();

Where ApiKeyProvider is:

public class ApiKeyProvider: IApiKeyProvider
{
    public string ApiKey { get; set; }
}

And inject into the typed client:

public class MyHttpClient
{
    public HttpClient Client { get; }

    public MyHttpClient(HttpClient client, IApiKeyProvider apiKeyProvider)
    {
        var key = apiKeyProvider.ApiKey;
    }
}

Or when the key is stored in the configuration then you can use IOptions.

In startup:

services.Configure<ApiSettings>(options => configuration.GetSection("ApiSettings").Bind(options));

Where ApiSettings is:

public class ApiSettings
{
    public string ApiKey { get; set; }
}

And inject into the typed client:

public class MyHttpClient
{
    public HttpClient Client { get; }

    public MyHttpClient(HttpClient client, IOptions<ApiSettings> apiSettings)
    {
        var key = apiSettings.Value.ApiKey;
    }
}

In the same way you can access the request context, like when you want to read the current access_token, as described in my answer here.

2 Comments

I know this question is a bit late, but what happens when you have 2 or more IApiKeyProvider 's registered? How can u say: "this specific typed HttpClient should use this specific IApiKeyProvider? For example, you have a typed client that does stuff against Google, another against Azure and finally the last one against AWS ... and all three of these have an API Key required, which is NOT part of the headers but as part of the actual payload (for example).
Usually you create a factory and use the key to get the specific implementation. See this answer: stackoverflow.com/questions/39174989/… But perhaps it is an option to extend ApiKeyProvider with additional keys, like ApiKeyGoogle, ApiKeyAWS, etc. instead of ApiKey. Where each client (typed or named) reads the appropriate key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.