温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET Core 中HttpClientFactory如何使用

发布时间:2021-07-15 14:35:35 阅读:276 作者:Leah 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章为大家展示了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 的工厂注册。

        // 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<stringGet()        {            var httpClient = httpClientFactory.CreateClient("IDGCustomApi");            string html = await httpClient.GetStringAsync("http://bing.com");            return html;        }    }
 
ASP.NET Core 中HttpClientFactory如何使用  

从 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;        }    }

上述内容就是ASP.NET Core 中HttpClientFactory如何使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/135083/blog/5024447

AI

开发者交流群×