阅读 216

NET CORE HttpClient的使用方法

这篇文章主要给大家介绍了关于.NET CORE HttpClient的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者使用.NET CORE具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

前言

自从HttpClient诞生依赖,它的使用方式一直备受争议,framework版本时代产生过相当多经典的错误使用案例,包括Tcp链接耗尽、DNS更改无感知等问题。有兴趣的同学自行查找研究。在.NETCORE版本中,提供了IHttpClientFactory用来创建HttpClient以解决之前的种种问题。那么我们一起看一下它的用法。

使用方式

  • 基本用法。 直接注入IHttpClientFactory

  • 命名客户端。注入IHttpClientFactory并带有名称,适用于需要特定的客户端配置

  • 类型化客户端。类似于命名客户端,但不需要名称作为标识,直接和某个服务类绑定在一起。注:这种方式经测试貌似不适用控制台程序。

  • 生成客户端。这种方式相当于在客户端生成对应的代理服务,一般特定的需要才需要这种方式。需要结合第三方库如 Refit 使用。这里不具体介绍。

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void ConfigureServices(IServiceCollection services)
{
 //普通注入
 serviceCollection.AddHttpClient();
 //命名注入
 serviceCollection.AddHttpClient(Constants.SERVICE_USERACCOUNT, (serviceProvider, c) =>
 {
  var configuration = serviceProvider.GetRequiredService<IConfiguration>();
 c.BaseAddress = new Uri(configuration.GetValue<string>("ServiceApiBaseAddress:UserAccountService"));
 });
 //类型化客户端
 services.AddHttpClient<TypedClientService>();
}
 
public class AccreditationService
{
 private IHttpClientFactory _httpClientFactory;
 private const string _officialAccreName = "manage/CommitAgencyOfficialOrder";
 private const string _abandonAccUserName = "info/AbandonUserAccreditationInfo";
 
 public AccreditationService(IHttpClientFactory clientFactory)
 {
  _httpClientFactory = clientFactory;
 }
 
 public async Task<string> CommitAgentOfficial(CommitAgencyOfficialOrderRequest request)
 {
    //使用factory 创建httpclient
   var httpClient = _httpClientFactory.CreateClient(Constants.SERVICE_ACCREDITATION);
   var response = await httpClient.PostAsJsonAsync(_officialAccreName, request);
   if (!response.IsSuccessStatusCode) return string.Empty;
   var result = await response.Content.ReadAsAsync<AccreditationApiResponse<CommitAgencyOfficialOrderResult>>();
   if (result.ReturnCode != "0") return string.Empty;
    return result.Data.OrderNo;
 }
}

命名化客户端方式直接注入的是HttpClient而非HttpClientFactory

1
2
3
4
5
6
7
8
9
public class TypedClientService
{
 private HttpClient _httpClient;
 
 public TypedClientService(HttpClient httpClient)
 {
  _httpClient = httpClient;
 }
}

Logging

通过IHttpClientFactory创建的客户端默认记录所有请求的日志消息,并每个客户端的日志类别会包含客户端名称,例如,名为 MyNamedClient 的客户端记录类别为“System.Net.Http.HttpClient.MyNamedClient.LogicalHandler”的消息。

请求管道

同framework时代的HttpClient一样支持管道处理。需要自定义一个派生自DelegatingHandler的类,并实现SendAsync方法。例如下面的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ValidateHeaderHandler : DelegatingHandler
{
 protected override async Task<HttpResponseMessage> SendAsync(
  HttpRequestMessage request,
  CancellationToken cancellationToken)
 {
  if (!request.Headers.Contains("X-API-KEY"))
  {
   return new HttpResponseMessage(HttpStatusCode.BadRequest)
   {
    Content = new StringContent(
     "You must supply an API key header called X-API-KEY")
   };
  }
 
  return await base.SendAsync(request, cancellationToken);
 }
}

在AddHttpClient的时候注入进去

1
2
3
4
5
6
7
8
9
10
11
public void ConfigureServices(IServiceCollection services)
{
 services.AddTransient<ValidateHeaderHandler>();
 
 services.AddHttpClient("externalservice", c =>
 {
  // Assume this is an "external" service which requires an API KEY
  c.BaseAddress = new Uri("https://localhost:5001/");
 })
 .AddHttpMessageHandler<ValidateHeaderHandler>();
}

原理和生存周期

IHttpClientFactory每次调用CreateHttpClient都会返回一个全新的HttpClient实例。而负责http请求处理的核心HttpMessageHandler将会有工厂管理在一个池中,可以重复使用,以减少资源消耗。HttpMessageHandler默认生成期为两分钟。可以在每个命名客户端上重写默认值:

1
2
3
4
5
public void ConfigureServices(IServiceCollection services)
{  
 services.AddHttpClient("extendedhandlerlifetime")
  .SetHandlerLifetime(TimeSpan.FromMinutes(5));
}

Polly支持

Polly是一款为.NET提供恢复能力和瞬态故障处理的库,它的各种策略应用(重试、断路器、超时、回退等)。IHttpClientFactory增加了对其的支持,它的nuget包为: Microsoft.Extensions.Http.Polly。注入方式如下:

1
2
3
4
5
6
7
public void ConfigureServices(IServiceCollection services)
{  
 services.AddHttpClient<UnreliableEndpointCallerService>()
  .AddTransientHttpErrorPolicy(p =>
   p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));
 
}

更详细的结合使用请参考:https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory

总结

到此这篇关于.NET CORE HttpClient使用方法的文章就介绍到这了


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐