本篇内容主要讲解“怎样在ASP.NET Core中使用HttpClientFactory”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎样在ASP.NET Core中使用HttpClientFactory”吧!
ASP.NET 是什么
ASP.NET 是开源,跨平台,高性能,轻量级的 Web 应用构建框架,常用于通过 HTML、CSS、JavaScript 以及服务器脚本来构建网页和网站。
为什么要使用 HttpClientFactory
可以用 HttpClientFactory 来集中化管理 HttpClient,工厂提供了对 HttpClient 的创建,配置和调度,值得一提的是:HttpClient 一直都是 Http 请求业务方面的一等公民。
HttpClient 虽好,但它有一些缺点:
所以推荐的做法是创建一个可供复用的共享式 HttpClient 实例,如果你要打破沙锅问到低的话,即使是创建共享式的 HttpClient 也会有很多问题,比如它会无视 DNS 缓存生效,那怎么办呢?可以用 .NET Core 2.1 引入的 HttpClientFactory 来解决此问题。。。用它来统一化的高效管理 HttpClient。
使用 HttpClientFactory
HttpClientFactory 有两种使用方式。
所谓的 NamedClient 就是注册带有标记的 HttpClient 到 HttpClientFactory 工厂中,下面的代码展示了一个名为 IDGCustomApi
的 HttpClient 的工厂注册。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("IDGCustomApi", client =>
{
client.BaseAddress = new Uri("https://localhost:6045/");
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", "IDG");
});
services.AddControllers();
}
所谓的 TypedClient 就是注册一个你自定义的 HttpClient,我想你肯定有点懵逼了,没关系,我现在就来自定义 HttpClient, 然后通过 AddHttpClient() 注册到容器中。
public class CustomHttpClient
{
public HttpClient Client { get; }
public CustomHttpClient(HttpClient client)
{
Client = client;
}
}
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<CustomHttpClient>(client => client.BaseAddress = new Uri("https://localhost:6045/"));
services.AddControllers();
}
}
注入 Controller
为了能够在 Controller 中使用,可以将 IHttpClientFactory 通过构造函数方式进行注入,参考如下代码:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private IHttpClientFactory httpClientFactory;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
[HttpGet]
public async Task<string> Get()
{
var httpClient = httpClientFactory.CreateClient("IDGCustomApi");
string html = await httpClient.GetStringAsync("http://bing.com");
return html;
}
}

从 IHttpClientFactory 的默认实现 DefaultHttpClientFactory 的源码也可以看出,httpClient 所关联的 HttpMessageHandler 和 Options 都被工厂跟踪和管控。
internal class DefaultHttpClientFactory : IHttpClientFactory, IHttpMessageHandlerFactory
{
public HttpClient CreateClient(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
HttpMessageHandler handler = CreateHandler(name);
HttpClient httpClient = new HttpClient(handler, disposeHandler: false);
HttpClientFactoryOptions httpClientFactoryOptions = _optionsMonitor.Get(name);
for (int i = 0; i < httpClientFactoryOptions.HttpClientActions.Count; i++)
{
httpClientFactoryOptions.HttpClientActions[i](httpClient);
}
return httpClient;
}
public HttpMessageHandler CreateHandler(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
ActiveHandlerTrackingEntry value = _activeHandlers.GetOrAdd(name, _entryFactory).Value;
StartHandlerEntryTimer(value);
return value.Handler;
}
}
译文链接:https://www.infoworld.com/article/3276007/how-to-work-with-httpclientfactory-in-aspnet-core.html
到此,相信大家对“怎样在ASP.NET Core中使用HttpClientFactory”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!