How to solve InvalidOperationException for constructors using HttpClientFactory in C#
A suitable constructor for type ‘X’ could not be located. What a strange error message! Luckily it’s easy to solve.
Table of Contents
Just a second! π«·
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .
If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.
Thank you for your understanding.
- Davide
A few days ago I was preparing the demo for a new article. The demo included a class with an IHttpClientFactory
service injected into the constructor. Nothing more.
Then, running the application (well, actually, executing the code), this error popped out:
System.InvalidOperationException: A suitable constructor for type ‘X’ could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
How to solve it? It’s easy. But first, let me show you what I did in the wrong version.
Setting up the wrong example
For this example, I created an elementary project.
It’s a .NET 7 API project, with only one controller, GenderController
, which calls another service defined in the IGenderizeService
interface.
public interface IGenderizeService
{
Task<GenderProbability> GetGenderProbabiliy(string name);
}
IGenderizeService
is implemented by a class, GenderizeService
, which is the one that fails to load and, therefore, causes the exception to be thrown. The class calls an external endpoint, parses the result, and then returns it to the caller:
public class GenderizeService : IGenderizeService
{
private readonly IHttpClientFactory _httpClientFactory;
public GenderizeService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<GenderProbability> GetGenderProbabiliy(string name)
{
var httpClient = _httpClientFactory.CreateClient();
var response = await httpClient.GetAsync($"?name={name}");
var result = await response.Content.ReadFromJsonAsync<GenderProbability>();
return result;
}
}
Finally, I’ve defined the services in the Program class, and then I’ve specified which is the base URL for the HttpClient instance generated in the GenderizeService
class:
// some code
builder.Services.AddScoped<IGenderizeService, GenderizeService>();
builder.Services.AddHttpClient<IGenderizeService, GenderizeService>(
client => client.BaseAddress = new Uri("https://api.genderize.io/")
);
var app = builder.Build();
// some more code
That’s it! Can you spot the error?
2 ways to solve the error
The error was quite simple, but it took me a while to spot:
In the constructor I was injecting an IHttpClientFactory
:
public GenderizeService(IHttpClientFactory httpClientFactory)
while in the host definition I was declaring an HttpClient
for a specific class:
builder.Services.AddHttpClient<IGenderizeService, GenderizeService>
Apparently, even if we’ve specified how to create an instance for a specific class, we could not build it using an IHttpClientFactory.
So, here are 2 ways to solve it.
Use named HttpClient in HttpClientFactory
Named HttpClients are a helpful way to define a specific HttpClient and use it across different services.
It’s as simple as assigning a name to an HttpClient instance and then using the same name when you need that specific client.
So, define it in the Startup method:
builder.Services.AddHttpClient("genderize",
client => client.BaseAddress = new Uri("https://api.genderize.io/")
);
and retrieve it using CreateClient
:
public GenderizeService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<GenderProbability> GetGenderProbabiliy(string name)
{
var httpClient = _httpClientFactory.CreateClient("genderize");
var response = await httpClient.GetAsync($"?name={name}");
var result = await response.Content.ReadFromJsonAsync<GenderProbability>();
return result;
}
π‘ Quick tip: define the HttpClient
names in a constant field shared across the whole system!
Inject HttpClient instead of IHttpClientFactory
The other way is by injecting an HttpClient
instance instead of an IHttpClientFactory
.
So we can restore the previous version of the Startup part:
builder.Services.AddHttpClient<IGenderizeService, GenderizeService>(
client => client.BaseAddress = new Uri("https://api.genderize.io/")
);
and, instead of injecting an IHttpClientFactory, we can directly inject an HttpClient
instance:
public class GenderizeService : IGenderizeService
{
private readonly HttpClient _httpClient;
public GenderizeService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<GenderProbability> GetGenderProbabiliy(string name)
{
//var httpClient = _httpClientFactory.CreateClient("genderize");
var response = await _httpClient.GetAsync($"?name={name}");
var result = await response.Content.ReadFromJsonAsync<GenderProbability>();
return result;
}
}
We no longer need to call _httpClientFactory.CreateClient
because the injected instance of HttpClient
is already customized with the settings we’ve defined at Startup.
Further readings
I’ve briefly talked about HttpClientFactory
in one article of my C# tips series:
π C# Tip: use IHttpClientFactory to generate HttpClient instance | Code4IT
And, more in detail, I’ve also talked about one way to mock HttpClientFactory
instances in unit tests using Moq:
π How to test HttpClientFactory with Moq | Code4IT
Finally, why do we need to use HttpClientFactories instead of HttpClients?
π Use IHttpClientFactory to implement resilient HTTP requests | Microsoft Docs
This article first appeared on Code4IT
Wrapping up
Yes, it was that easy!
We received the error message
A suitable constructor for type ‘X’ could not be located.
because we were mixing two ways to customize and use HttpClient
instances.
But we’ve only opened Pandora’s box: we will come back to this topic soon!
For now, Happy coding!
π§