如何在 C 语言中使用 HttpClientFactory 解决构造函数的
2022/11/11 14:53:54
本文主要是介绍如何在 C 语言中使用 HttpClientFactory 解决构造函数的,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
System.InvalidOperationException:找不到类型“X”的合适构造函数。确保类型是具体的,并且公共构造函数的所有参数都注册为服务或作为参数传递。还要确保没有提供无关的参数。
如何解决?这很容易。但首先,让我向您展示我在错误版本中做了什么。
设置错误的示例
对于此示例,我创建了一个基本项目。
这是一个 .NET 7 API 项目,只有一个控制器,它调用接口中定义的另一个服务。GenderController
IGenderizeService
public interface IGenderizeService { Task<GenderProbability> GetGenderProbabiliy(string name); }
IGenderizeService
由一个类实现,该类是加载失败的类,因此会导致引发异常。该类调用外部终结点,分析结果,然后将其返回给调用方:GenderizeService
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; } }
最后,我在 Program 类中定义了服务,然后指定了哪个是在类中生成的 HttpClient 实例的基本 URL:GenderizeService
// 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 for a specific class:HttpClient
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; }
这篇关于如何在 C 语言中使用 HttpClientFactory 解决构造函数的的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享
- 2024-11-22ansible 的archive 参数是什么意思?-icode9专业技术文章分享
- 2024-11-22ansible 中怎么只用archive 排除某个目录?-icode9专业技术文章分享
- 2024-11-22exclude_path参数是什么作用?-icode9专业技术文章分享
- 2024-11-22微信开放平台第三方平台什么时候调用数据预拉取和数据周期性更新接口?-icode9专业技术文章分享
- 2024-11-22uniapp 实现聊天消息会话的列表功能怎么实现?-icode9专业技术文章分享
- 2024-11-22在Mac系统上将图片中的文字提取出来有哪些方法?-icode9专业技术文章分享
- 2024-11-22excel 表格中怎么固定一行显示不滚动?-icode9专业技术文章分享
- 2024-11-22怎么将 -rwxr-xr-x 修改为 drwxr-xr-x?-icode9专业技术文章分享
- 2024-11-22在Excel中怎么将小数向上取整到最接近的整数?-icode9专业技术文章分享