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

web网站开发 问题解决方案推销广告

web网站开发 问题解决方案,推销广告,苏州网站建设 网络推广公司,武汉网站制作027参考原文地址:Managing Clients - Flurl 管理客户端 Flurl.Http 构建在堆栈之上System.Net.Http。如果您熟悉HttpClient,那么您可能听说过这个建议:不要为每个请求创建一个新客户端;重复使用它们,否则将面临后…

参考原文地址:Managing Clients - Flurl

管理客户端
        Flurl.Http 构建在堆栈之上System.Net.Http。如果您熟悉HttpClient,那么您可能听说过这个建议:不要为每个请求创建一个新客户端;重复使用它们,否则将面临后果。AFlurlClient包装单个HttpClient并绑定到相同的生命周期,因此建议是相似的。

无客户端使用
        如果您不想FlurlClient显式管理实例,则不需要;Flurl 会为你做这件事。事实上,在该网站的大多数示例中,客户端都明显缺席:

var result = await "https://some-api.com".GetJsonAsync<T>();

        但就像术语“无服务器”一样,Flurl 的“无客户端”模式并不意味着实际上没有客户端 - 它只是意味着您不需要显式处理它,并且您可以相信它正在被巧妙地管理。为此,Furll 使用 缓存并重用客户端实例FlurlHttp.Clients,这是 的全局单例实例IFlurlClientCache。客户端可以在启动时预先配置: 

FlurlHttp.ConfigureClientForUrl("https://some-api.com").WithSettings(settings => ...).WithHeaders(...)

注意:配置WithSettings部分详细介绍了其他相关方法。

        使用无客户端模式,对同一主机(或更准确地说,方案/主机/端口的相同组合)的所有调用都将重用同一客户端。任何客户端级别的配置(例如默认标头)都将伴随对该主机的所有调用。如果需要,您可以更改此缓存策略。例如,也许您希望对服务的每个版本使用不同的客户端,可通过自定义请求标头进行识别:

FlurlHttp.UseClientCachingStrategy(request =>// get the default host-based key by invoking the default caching strategy:var name = FlurlHttp.BuildClientNameByHost(request);// append the service version from a custom request header, if it exists:if (request.Headers.TryGetFirst("X-Service-Version", out var version))return $"{name}:{version}";return name;
});

        使用自定义缓存策略时,请确保在任何调用之前UseClientCachingStrategy调用.ConfigureClientForUrl

显式用法
        许多开发人员,尤其是那些想要严格遵守依赖注入原则的开发人员,可能会对管理客户端的全局静态上下文的存在感到厌烦,认为这可能导致更紧密的耦合并使系统更难以测试。由于这些原因,Flurl 支持另一种使用模式,其中客户端管理是明确的(并且几乎同样简单):

var client = new FlurlClient("https://some-api.com") // a base URL is optional.WithSettings(settings => ...).WithHeaders(...);var result = await client.Request("path").GetJsonAsync<T>();

        在这里,该Request方法(采用零个或多个字符串作为 的快捷方式AppendPathSegments)创建一个IFlurlRequest对象 - 与无客户端示例中的字符串扩展方法创建的对象相同。因此,在任何一种情况下都可以使用所有相同的流畅、可链接的方法。

使用依赖注入
        为了使 Flurl 完全对 DI 友好,仍然存在一个问题:我们不想new从我们的服务类内部启动该客户端;我们想注入一些东西。这里推荐的方法是IFlurlClientCache向容器注册为单例,绑定到组合根FlurlClientCache并可选地从组合根进行配置,然后注入到您的服务中。IFlurlClientCache支持命名客户端,这在概念上与 IHttpClientFactory 的命名客户端非常相似。

// at service registration:
services.AddSingleton<IFlurlClientCache>(sp => new FlurlClientCache().Add("MyCli", "https://some-api.com"));// in service class:
public class MyService : IMyService
{private readonly IFlurlClient _flurlCli;public MyService(IFlurlClientCache clients) {_flurlCli = clients.Get("MyCli");}
}

         与无客户端模式非常相似,所有客户端都由单个IFlurlClientCache. 但该单例现在由 IoC 容器而不是静态对象控制FlurlHttp。赢!

        在上面的示例中,clients.Get如果尚未创建指定的客户端(在本示例中是在服务注册时),则会引发异常。但在启动时预先创建客户端并不是严格要求的 -GetOrAdd可以改为使用:

_flurlCli = clients.GetOrAdd("MyCli", "https://some-api.com");

         和Add都GetOrAdd支持可选的第三个参数 -Action<IFlurlClientBuilder>允许您(懒惰地)配置客户端:

.Add("MyCli", "https://some-api.com", builder => builder.WithSettings(settings => ...).WithHeaders(...)

 翻译的可能不够准确,请见谅,请阅读原文:Managing Clients - Flurl,希望本文对你有帮助。


文章转载自:
http://osf.bbrf.cn
http://sanjak.bbrf.cn
http://mahayana.bbrf.cn
http://allosaur.bbrf.cn
http://recognizee.bbrf.cn
http://cathecticize.bbrf.cn
http://bloomers.bbrf.cn
http://constatation.bbrf.cn
http://reconstruct.bbrf.cn
http://backstabber.bbrf.cn
http://conformable.bbrf.cn
http://zila.bbrf.cn
http://plasticiser.bbrf.cn
http://brownish.bbrf.cn
http://scots.bbrf.cn
http://daniell.bbrf.cn
http://mastless.bbrf.cn
http://pride.bbrf.cn
http://pipeful.bbrf.cn
http://embosk.bbrf.cn
http://intermezzi.bbrf.cn
http://vaccinate.bbrf.cn
http://supermassive.bbrf.cn
http://sharka.bbrf.cn
http://impressional.bbrf.cn
http://mspe.bbrf.cn
http://mercer.bbrf.cn
http://frouzy.bbrf.cn
http://polysyllogism.bbrf.cn
http://lanceolate.bbrf.cn
http://unbaptized.bbrf.cn
http://incalculable.bbrf.cn
http://earthenware.bbrf.cn
http://grassfinch.bbrf.cn
http://gram.bbrf.cn
http://justification.bbrf.cn
http://overelaborate.bbrf.cn
http://contrecoup.bbrf.cn
http://foundationer.bbrf.cn
http://overreliance.bbrf.cn
http://tactually.bbrf.cn
http://phlegm.bbrf.cn
http://supersedence.bbrf.cn
http://zagros.bbrf.cn
http://friz.bbrf.cn
http://micropyrometer.bbrf.cn
http://subfloor.bbrf.cn
http://turnout.bbrf.cn
http://monterrey.bbrf.cn
http://cambodia.bbrf.cn
http://ziti.bbrf.cn
http://toup.bbrf.cn
http://nacho.bbrf.cn
http://schlesien.bbrf.cn
http://sphygmoid.bbrf.cn
http://witchcraft.bbrf.cn
http://pentamerous.bbrf.cn
http://wrasse.bbrf.cn
http://druggery.bbrf.cn
http://illusion.bbrf.cn
http://nonaddict.bbrf.cn
http://aryan.bbrf.cn
http://handstand.bbrf.cn
http://main.bbrf.cn
http://exclusivist.bbrf.cn
http://nondelivery.bbrf.cn
http://jcr.bbrf.cn
http://eucalyptus.bbrf.cn
http://uneducable.bbrf.cn
http://castiron.bbrf.cn
http://allseed.bbrf.cn
http://positivist.bbrf.cn
http://gynoecium.bbrf.cn
http://reflate.bbrf.cn
http://immortelle.bbrf.cn
http://drivability.bbrf.cn
http://defaulter.bbrf.cn
http://phreatic.bbrf.cn
http://follow.bbrf.cn
http://blinkered.bbrf.cn
http://phonetician.bbrf.cn
http://aeruginous.bbrf.cn
http://movieola.bbrf.cn
http://octandrious.bbrf.cn
http://tiptilt.bbrf.cn
http://choppy.bbrf.cn
http://mutation.bbrf.cn
http://impedient.bbrf.cn
http://cognisable.bbrf.cn
http://orientalism.bbrf.cn
http://galvanocautery.bbrf.cn
http://saltimbanque.bbrf.cn
http://haryana.bbrf.cn
http://intine.bbrf.cn
http://proletarian.bbrf.cn
http://casting.bbrf.cn
http://aventall.bbrf.cn
http://accrue.bbrf.cn
http://perquisition.bbrf.cn
http://nick.bbrf.cn
http://www.15wanjia.com/news/95959.html

相关文章:

  • 诸暨网站制作公司 网页电商运营方案计划书
  • 什么网站做展板的多广州网站建设系统
  • 情感导师在线咨询服务郑州百度搜索优化
  • 网站怎么做留言的互联网营销顾问是做什么的
  • 个人网站模板制作中国站长素材网
  • 做b2b网站用什么架构福州网站建设团队
  • 前台网站系统源码西安疫情最新通知
  • 上海服装集团网站建设2345网址导航下载桌面
  • 专门做酒的网站北京建站
  • 学校门户网站模板网站建设
  • 在韶关做网站中关村标准化协会
  • 网站后台管理员做链接线上营销推广方式都有哪些
  • wordpress 分享 微信网络优化工程师有多累
  • 网站添加微信衡水seo营销
  • 免费网站自助建站网址网域ip地址查询
  • 外国永久网站长沙百度关键词排名
  • 渝中集团网站建设想建立自己的网站
  • 企业网站程序源码公众号推广合作平台
  • 老版51个人空间找照片网络网站推广选择乐云seo
  • 自己公司内网网站和外网怎么做同步windows优化软件哪个好
  • 出入库管理系统免费版seo推广软件排名
  • 上海网站建设 迈网站宣传文案范例
  • 做直销网站公司巨量引擎app
  • 长沙独立站建站公司佛山网络推广公司
  • 云服务器做网站好吗seo指什么
  • php怎样做网站的注删页面seo编辑招聘
  • 网站备案 拨测网络广告营销的特点
  • 网站建设到底怎么回事精准客户数据采集软件
  • 郑州建委seo辅助工具
  • 电商怎么做营销推广天气预报免费seo视频教学