问题
I am trying to create a service that is accessible by both Controllers and regular functions for all API calls to a specific endpoint for my ASP.Net Core 2.2 application. Whenever I try and inject the HttpClient class an error occurs before the functions can be reached/
I have setup my startup.cs as shown in documentation and have reduced the constructor inputs to just HttpClient so I know that's where the root of the issue is. I have added the HttpClient to the configure services section fo the Startup.cs class every way I can think of.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
...
// adding ability to make basic http calls
services.AddHttpClient<IServerInventoryService, ServerInventoryService>(c =>
{
c.BaseAddress = new Uri("https://serverinventory.cooperatorsgroup.ca/api/");
}).ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler()
{
AllowAutoRedirect = false,
UseDefaultCredentials = true
};
});
}
Controller:
private ILogger<ServerInventoryApiController> logger;
private IHttpClientFactory clientFactory;
private readonly IServerInventoryService ServerInventoryService;
public ServerInventoryApiController(ILogger<ServerInventoryApiController> _logger, IServerInventoryService _service, IHttpClientFactory httpClientFactory)
{
ServerInventoryService = _service;
logger = _logger;
clientFactory = httpClientFactory;
}
APIService:
public class ServerInventoryService : IServerInventoryService
{
private HttpClient client;
// private readonly IConfiguration configuration;
public ServerInventoryService(HttpClient httpClient)
{
client = httpClient;
}
Everytime a call is made to the Service result is:
"InvalidOperationException: A suitable constructor for type 'ApplicationInventory.Services.ServerInventoryService' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor."
来源:https://stackoverflow.com/questions/56907365/a-suitable-constructor-for-type-applicationinventory-services-serverinventoryse