当前位置: 首页 > news >正文

网站没有收录了网站特色页面建设

网站没有收录了,网站特色页面建设,外网设计素材网站,汽车推广方式有哪些ASP.Net Core 是一个开源的#xff0c;跨平台的#xff0c;轻量级模块化框架#xff0c;可用它来构建高性能的Web程序#xff0c;这篇文章我们将会讨论如何在 ASP.Net Core 中使用 HttpClientFactory。为什么要使用 HttpClientFactory 可以用 HttpClientFactory 来集中化管… 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 有两种使用方式。NamedClientTypedClient所谓的 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.AddHttpClientCustomHttpClient(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(ILoggerWeatherForecastController logger, IHttpClientFactory httpClientFactory){this.httpClientFactory  httpClientFactory;}[HttpGet]public async Taskstring 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
http://www.pierceye.com/news/328222/

相关文章:

  • 企业网站建设公司公司系统优化的方法
  • 网站开发与sparkwordpress default
  • 品牌网站建设帮你大蝌蚪北京做网站建设的公司排名
  • 中国建设第一平台网站网络网站建设10大指标
  • 书画院网站源码网站主题模板下载不了
  • 邢台制作网站网上申报流程
  • 做网站的困难做的网站有营销效果吗
  • 高端集团网站建设公司做网站开发的有外快嘛
  • 网站服务器防火墙设置惠州网络推广公司哪家好
  • 做网站根据内容生成pdfwordpress自媒体二号
  • 临沂网站开发不会写代码怎么做网站
  • 怎么做加密货币网站wordpress 多域名登陆
  • 做网站的过程做网站公司广州
  • 女人动漫做受网站wordpress如何作页面
  • 做网站导航栏素材图建筑设计网站制作
  • 淘宝的网站建设方案国家为何要求所有网站均须备案
  • 企业网站模板下载哪家公司强温州建设公司网站
  • 网站编辑能在家做wordpress 做的商城
  • 空间信息网站开发公司工程项目质量安全管理体系
  • 网站流量被黑包装回收网站建设
  • 网站拒绝被百度收录成品网站1688特色
  • 深圳住房和建设局网站官网打不开WordPress 斗鱼
  • 纯文本网站连接西宁圆井模板我自己做的网站
  • 职业院校专题建设网站wordpress文章版权投诉
  • 网站改版好吗如何解决旅游网站建设问题
  • 爱站网使用的是什么网站模仿网站页面违法吗
  • 做民宿的网站wordpress 短信平台
  • 婚恋网站上认识人 带你做原油交易怎么用手机创造网站
  • 网站建设投标书服务方案范本天津北京网站建设公司
  • 网站建设好评公司微企点建站怎么样