[英]Using named HttpClient from IHttpClientFactory in a custom client
I know I'll get crucified for asking this question that has been asked a million times before and I promise you that I've looked at most of those questions/answers but I'm still a bit stuck.我知道我会因为问这个问题而被钉在十字架上,这个问题之前已经被问过一百万次了,我 promise 你,我已经看过其中的大部分问题/答案,但我仍然有点卡住了。
This is a .NET Standard 2.0 class library supporting an ASP.NET Core 6 API.这是一个 .NET 标准 2.0 class 库,支持 ASP.NET Core 6 API。
In my Program.cs
I create a named HttpClient like this:在我的
Program.cs
中,我创建了一个命名的 HttpClient,如下所示:
builder.Services.AddHttpClient("XYZ_Api_Client", config =>
{
var url = "https://example.com/api";
config.BaseAddress = new Uri(url);
});
I have a custom client that will use this HttpClient
and I create a singleton MyCustomClient
in Program.cs
so that my repositories can use it.我有一个将使用此
HttpClient
的自定义客户端,我在Program.cs
中创建了一个 singleton MyCustomClient
,以便我的存储库可以使用它。 The code is below.代码如下。 This is where I'm stuck as I'm not sure how to pass my named
HttpClient
into MyCustomClient
.这是我卡住的地方,因为我不确定如何将我命名的
HttpClient
传递到MyCustomClient
中。
builder.Services.AddSingleton(new MyCustomClient(???)); // I think I need to pass the HttpClient to my CustomClient here but not sure how
And my CustomClient
needs to use this HttpClient
named XYZ_Api_Client
to do its job:我的
CustomClient
需要使用这个名为XYZ_Api_Client
的HttpClient
来完成它的工作:
public class MyCustomClient
{
private readonly HttpClient _client;
public MyCustomClient(HttpClient client)
{
_client = client;
}
public async Task<bool> DoSomething()
{
var result = await _client.GetAsync();
return result;
}
}
So I'm not sure how I can pass this named HttpClient
into MyCustomClient
in the Program.cs
.所以我不确定如何将这个命名的
HttpClient
传递到Program.cs
中的MyCustomClient
中。
You can directly inject the IHttpClientFactory
in you class, and then assign the named HttpClient
to a property.你可以直接在你class中注入
IHttpClientFactory
,然后将命名的HttpClient
赋值给一个属性。
Register the factory and your custom client:注册工厂和您的自定义客户端:
builder.Services.AddHttpClient("XYZ_Api_Client", config =>
{
var url = "https://example.com/api";
config.BaseAddress = new Uri(url);
});
// no need to pass anything, the previous line registered IHttpClientFactory in the container
builder.Services.AddSingleton<MyCustomClient>();
And then in you class:然后在你 class 中:
public class MyCustomClient
{
private readonly HttpClient _client;
public MyCustomClient(IHttpClientFactory factory)
{
_client = factory.CreateClient("XYZ_Api_Client");
}
// ...
}
Or, you can pass the named instance when registering MyCustomClient
或者,您可以在注册
MyCustomClient
时传递命名实例
Register the factory and your custom client:注册工厂和您的自定义客户端:
builder.Services.AddHttpClient("XYZ_Api_Client", config =>
{
var url = "https://example.com/api";
config.BaseAddress = new Uri(url);
});
// specify the factory for your class
builder.Services.AddSingleton<MyCustomClient>(sp =>
{
var factory = sp.GetService<IHttpClientFactory>();
var httpClient = factory.CreateClient("XYZ_Api_Client");
return new MyCustomClient(httpClient);
});
And then in you class:然后在你 class 中:
public class MyCustomClient
{
private readonly HttpClient _client;
public MyCustomClient(HttpClient client)
{
_client = client;
}
// ...
}
You can also do this:您也可以这样做:
// register the named client with the name of the class
builder.Services.AddHttpClient("MyCustomClient", config =>
{
config.BaseAddress = new Uri("https://example.com/api");
});
// no need to specify the name of the client
builder.Services.AddHttpClient<MyCustomClient>();
What AddHttpClient<TClient>(IServiceCollection)
does is AddHttpClient<TClient>(IServiceCollection)
所做的是
Adds the IHttpClientFactory and related services to the IServiceCollection and configures a binding between the TClient type and a named HttpClient.
将 IHttpClientFactory 和相关服务添加到 IServiceCollection 并配置 TClient 类型和命名的 HttpClient 之间的绑定。 The client name will be set to the full name of TClient.
客户端名称将设置为 TClient 的全名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.