ASP.Net Core 是一个开源的,跨平台的,轻量级模块化框架,可用它来构建高性能的Web程序,这篇文章我们将会讨论如何在 ASP.Net Core 中使用 HttpClientFactory。
为什么要使用 HttpClientFactory
可以用 HttpClientFactory 来集中化管理 HttpClient,工厂提供了对 HttpClient 的创建,配置和调度,值得一提的是:HttpClient 一直都是 Http 请求业务方面的一等公民。
HttpClient 虽好,但它有一些缺点:
- 创建太多的 HttpClient 是一种低效的行为,因为当一个新客户端连接到远程 Server 时,你的应用程序还需要承担着重连远程 Server 的开销。
- 如果每一个 request 都创建一个 HttpClient,当应用程序负载过大, Socket 必将耗尽,比如默认情况下 HttpClient 会维持至少4分钟的 Connection 连接。
所以推荐的做法是创建一个可供复用的共享式 HttpClient 实例,如果你要打破沙锅问到低的话,即使是创建共享式的 HttpClient 也会有很多问题,比如它会无视 DNS 缓存生效,那怎么办呢?可以用 .NET Core 2.1 引入的 HttpClientFactory 来解决此问题。。。用它来统一化的高效管理 HttpClient。
使用 HttpClientFactory
HttpClientFactory 有两种使用方式。
- NamedClient
- TypedClient
所谓的 NamedClient 就是注册带有标记的 HttpClient 到 HttpClientFactory 工厂中,下面的代码展示了一个名为 IDGCustomApi 的 HttpClient 的工厂注册。
//Thismethodgetscalledbytheruntime.Usethismethodtoaddservicestothecontainer.
publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddHttpClient("IDGCustomApi",client=>
{
client.BaseAddress=newUri("https://localhost:6045/");
client.DefaultRequestHeaders.Add("Accept","application/json");
client.DefaultRequestHeaders.Add("User-Agent","IDG");
});
services.AddControllers();
}
所谓的 TypedClient 就是注册一个你自定义的 HttpClient,我想你肯定有点懵逼了,没关系,我现在就来自定义 HttpClient, 然后通过 AddHttpClient() 注册到容器中。
publicclassCustomHttpClient
{
publicHttpClientClient{get;}
publicCustomHttpClient(HttpClientclient)
{
Client=client;
}
}
publicclassStartup
{
//Thismethodgetscalledbytheruntime.Usethismethodtoaddservicestothecontainer.
publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddHttpClient<CustomHttpClient>(client=>client.BaseAddress=newUri("https://localhost:6045/"));
services.AddControllers();
}
}
注入 Controller
为了能够在 Controller 中使用,可以将 IHttpClientFactory 通过构造函数方式进行注入,参考如下代码:
[ApiController]
[Route("[controller]")]
publicclassWeatherForecastController:ControllerBase
{
privateIHttpClientFactoryhttpClientFactory;
publicWeatherForecastController(ILogger<WeatherForecastController>logger,IHttpClientFactoryhttpClientFactory)
{
this.httpClientFactory=httpClientFactory;
}
[HttpGet]
publicasyncTask<string>Get()
{
varhttpClient=httpClientFactory.CreateClient("IDGCustomApi");
stringhtml=awaithttpClient.GetStringAsync("http://bing.com");
returnhtml;
}
}
从 IHttpClientFactory 的默认实现 DefaultHttpClientFactory 的源码也可以看出,httpClient 所关联的 HttpMessageHandler 和 Options 都被工厂跟踪和管控。
internalclassDefaultHttpClientFactory:IHttpClientFactory,IHttpMessageHandlerFactory
{
publicHttpClientCreateClient(stringname)
{
if(name==null)
{
thrownewArgumentNullException("name");
}
HttpMessageHandlerhandler=CreateHandler(name);
HttpClienthttpClient=newHttpClient(handler,disposeHandler:false);
HttpClientFactoryOptionshttpClientFactoryOptions=_optionsMonitor.Get(name);
for(inti=0;i<httpClientFactoryOptions.HttpClientActions.Count;i++)
{
httpClientFactoryOptions.HttpClientActions[i](httpClient);
}
returnhttpClient;
}
publicHttpMessageHandlerCreateHandler(stringname)
{
if(name==null)
{
thrownewArgumentNullException("name");
}
ActiveHandlerTrackingEntryvalue=_activeHandlers.GetOrAdd(name,_entryFactory).Value;
StartHandlerEntryTimer(value);
returnvalue.Handler;
}
}
译文链接:https://www.infoworld.com/article/3276007/how-to-work-with-httpclientfactory-in-aspnet-core.html
以上就是如何在ASP.NET Core中使用HttpClientFactory的详细内容,更多关于ASP.NET Core使用HttpClientFactory的资料请关注其它相关文章!




