如何解决我的Wep API服务中的“错误注入IHttpClientFactory,没有匹配的绑定可用”错误

时间:2019-02-08 20:53:25

标签: c# http

我正在使用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.
  }
}

请提出如何解决此问题的建议。

0 个答案:

没有答案