我在Startup.cs类中创建了一个这样的客户端。
services.AddHttpClient("myClient", client =>
{
client.BaseAddress = new Uri("https://test.com");
});然后我有一个客户端文件Client.cs
protected readonly HttpClient httpClient;
public Client(HttpClient httpClient)
{
this.httpClient = httpClient;
}现在,当我在ServiceClass.cs中扩展客户端类时
private readonly HttpClient _myClient;
public ServiceClass(IHttpClientFactory httpClientFactory) : base(//What should I pass here?)
{
_myClient = httpClientFactory.CraeteClient("myClient");
}我不确定我应该在这里传递什么对象,或者我的代码结构不正确。请帮帮我!
发布于 2021-02-03 10:57:58
由于您有一个受保护的字段httpClient,因此完全删除_myClient字段,并让基构造器处理它:
public ServiceClass(IHttpClientFactory httpClientFactory) :
base(httpClientFactory.CraeteClient("myClient"))
{}代码假定您不是在寻找两个不同的HttpClient成员。
https://stackoverflow.com/questions/66020540
复制相似问题