我正在使用Web ApiService。我正在修改服务以使用IHttpClientFactory
创建HttpClient
的实例,而不是为每个服务请求创建HttpClient
实例。
我在ConfigureServices
中添加了startup.cs
方法。并将IHttpClientFactory
注入到ServiceFacade.cs
(位于应用程序层中)中,这是我想要使用HttpClient
调用主服务中另一组服务的地方。
我的startup.cs
文件:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
ServiceFacade.cs:
public LoanLedgerServiceFacade(IFileMapper fileMapper, IHttpClientFactory httpClientFactory)
{
_fileMapper = fileMapper;
_httpClientFactory = httpClientFactory;
}
public async Task<IFile> GetFileInformation(string fileNumber, string clientUser)
{
IFile fileInformation;
HttpResponseMessage response;
var serviceUriPattern = string.Format(@"{0}/{1}", _serviceUri, _servicePath);
var httpClient = _httpClientFactory.CreateClient();
using (var client = httpClient)
{
var serviceUriWithQueryString = new Uri(string.Format(serviceUriPattern, fileNumber));
response = await client.GetAsync(serviceUriWithQueryString);
}
if (response.IsSuccessStatusCode)
{
FileContract fileContract = await response.Content.ReadAsAsync<FileContract>();
fileInformation = _fileMapper.ToModel(fileContract);
}
return fileInformation;
}
我遇到以下错误:
{
"message": "An error has occurred.",
"exceptionMessage": "An error occurred when trying to create a controller of type 'TransactionController'. Make sure that the controller has a parameterless public constructor.",
"exceptionType": "System.InvalidOperationException",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "Error activating IHttpClientFactory\r\nNo matching bindings are available, and the type is not self-bindable.\r\nActivation path:\r\n 6) Injection of dependency IHttpClientFactory into parameter httpClientFactory of constructor of type ServiceFacade\r\n 5) Injection of dependency IServiceFacade into parameter ServiceFacade of constructor of type TransactionApplication\r\n 4) Injection of dependency ITransactionApplication into parameter application of constructor of type TransactionController\r\n 3) Injection of dependency TransactionController into parameter resolutionRoot of constructor of type NamedScope\r\n.
}
}
请提出如何解决此问题的建议。