In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how ASP.NET Core uses IHttpClientFactory to send HTTP requests. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Problems in the use of 1.HttpClient class
There are a lot of articles searched by Baidu about the problems existing in the use of HttpClient class, many of which are simple text descriptions, which make people feel difficult to understand. In order to better understand the problems in the use of HttpClient, let's describe them through code and examples.
Using (var client = new HttpClient ())
The traditional method of closing the connection is shown in the above code, but when the HttpClient object is released using the using statement, the socket will not be released immediately. Let's verify it by requesting an example of an aspnetmonsters site:
Class Program {static void Main (string [] args) {Console.WriteLine ("Starting connections"); var g = GetAsync (); g.Wait (); Console.WriteLine ("Connections done"); Console.ReadKey ();} static async Task GetAsync () {for (int I = 0; I)
< 5; i++) { using (var client = new HttpClient()) { var result = await client.GetAsync("http://aspnetmonsters.com/"); Console.WriteLine(result.StatusCode); } } }} 输出结果:The console prints out five pieces of information requesting the return status of the site. Let's print out five request connection socket states through the netstat tool:
The application has finished running (ending the connection), but the print shows that the connection status is still TIME_WAIT, which means that during this state, you are still watching to see if any packets enter the connection (if any packets are still passing through while the connection is waiting), because they may be delayed by the network somewhere.
Windows will remain connected for 240 seconds in this state (set by it [HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Services\ Tcpip\ Parameters\ TcpTimedWaitDelay]). The speed at which Windows can quickly open new sockets is limited, so if you run out of connection pooling, you may see the following error:
And what can be done to reduce socket waste? In the above code, we pull the HttpClient object created in each loop outside the Main and define it as a shared static instance:
Class Program {private static HttpClient client = new HttpClient (); static void Main (string [] args) {Console.WriteLine ("Starting connections"); var g = GetAsync (); g.Wait (); Console.WriteLine ("Connections done"); Console.ReadKey ();} static async Task GetAsync () {for (int I = 0; I)
< 5; i++) { var result = await client.GetAsync("http://aspnetmonsters.com/"); Console.WriteLine(result.StatusCode); } }} 应用程序运动完毕之后,我们再通过netstat工具打印出五个请求连接套接字状态,这时候会看到信息如下: 通过共享一个实例,减少了套接字的浪费,实际上由于套接字重用而传输快一点。 总结: 在创建HttpClient实例的时候,最好是静态(static )实例。 不要用using包装HttpClient对象。 在.NET Core 2.1版本之后引入的 HttpClientFactory解决了HttpClient的所有痛点。有了 HttpClientFactory,我们不需要关心如何创建HttpClient,又如何释放它。通过它可以创建具有特定业务的HttpClient,而且可以很友好的和 DI 容器结合使用,更为灵活。下面以 ASP.NET Core为例介绍HttpClientFactory的四种使用方式。 2.HttpClientFactory 的多种使用方式 可以通过多种使用方式在应用程序中使用HttpClientFactory。 2.1使用基本用法 在Startup.ConfigureServices方法中,通过在IServiceCollection上调用AddHttpClient扩展方法可以注册IHttpClientFactory服务。 services.AddHttpClient(); 注册服务后,我们新建BasicUsageModel类使用IHttpClientFactory创建HttpClient实例: public class BasicUsageModel{ private readonly IHttpClientFactory _clientFactory; public IEnumerable Branches { get; private set; } public bool GetBranchesError { get; private set; } public BasicUsageModel(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; } public async Task OnGet() { var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/aspnet/AspNetCore.Docs/branches"); request.Headers.Add("Accept", "application/vnd.github.v3+json"); request.Headers.Add("User-Agent", "HttpClientFactory-Sample"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { Branches = await response.Content .ReadAsAsync(); } else { GetBranchesError = true; Branches = Array.Empty(); } }}public class GitHubBranch{ public string name { get; set; }} 以这种方式直接在使用IHttpClientFactory的类中调用CreateClient方法创建HttpClient实例。然后在Controller中调用BasicUsageModel类: public class HomeController : Controller{ private readonly IHttpClientFactory _clientFactory; public HomeController(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; } public IActionResult Index() { BasicUsageModel model = new BasicUsageModel(_clientFactory); var task = model.OnGet(); task.Wait(); List list = model.Branches.ToList(); return View(list); }}2.2使用命名客户端 如果应用程序需要有许多不同的HttpClient用法(每种用法的服务配置都不同),可以视情况使用命名客户端。可以在HttpClient中注册时指定命名Startup.ConfigureServices的配置。 services.AddHttpClient("github", c =>{c.BaseAddress = new Uri ("https://api.github.com/"); / / Github API versioning c.DefaultRequestHeaders.Add (" Accept "," application/vnd.github.v3+json "); / / Github requires a user-agent c.DefaultRequestHeaders.Add (" User-Agent "," HttpClientFactory-Sample ");})
The above code calls AddHttpClient and provides the name "github". This client applies some default configuration, which requires a base address and two headers to use GitHub API. Each time CreateClient is called, a new instance of HttpClient is created and the configuration operation is invoked. To use a named client, pass a string parameter to CreateClient. Specify the name of the client to create:
Public class NamedClientModel: PageModel {private readonly IHttpClientFactory _ clientFactory; public IEnumerable PullRequests {get; private set;} public bool GetPullRequestsError {get; private set;} public bool HasPullRequests = > PullRequests.Any (); public NamedClientModel (IHttpClientFactory clientFactory) {_ clientFactory = clientFactory;} public async Task OnGet () {var request = new HttpRequestMessage (HttpMethod.Get, "repos/aspnet/AspNetCore.Docs/pulls") Var client = _ clientFactory.CreateClient ("github"); var response = await client.SendAsync (request); if (response.IsSuccessStatusCode) {PullRequests = await response.Content .ReadAsync ();} else {GetPullRequestsError = true; PullRequests = Array.Empty ();} public class GitHubPullRequest {public string url {get Set;} public int? Id {get; set;} public string node_id {get; set;}}
In the above code, the request does not need to specify a hostname. You can pass only the path because the base address configured for the client is used. Call the method in Controller as in the previous example.
2.3 using typed clients
What is a typed client? It is just the HttpClient configured when DefaultHttpClientFactory is injected.
The following figure shows how to use a typed client with HttpClientFactory:
Typed clients provide the same functionality as named clients and do not need to use a string as a key. They provide a single address to configure and interact with a particular HttpClient. For example, a single typed client might be used for a single back-end endpoint and encapsulate all the processing logic for that endpoint. Another advantage is that they use DI and can be injected into the desired location in the application.
The typed client receives the HttpClient parameter in the constructor:
Public class GitHubService {public HttpClient Client {get;} public GitHubService (HttpClient client) {client.BaseAddress = new Uri ("https://api.github.com/"); / / GitHub API versioning client.DefaultRequestHeaders.Add (" Accept "," application/vnd.github.v3+json "); / / GitHub requires a user-agent client.DefaultRequestHeaders.Add (" User-Agent "," HttpClientFactory-Sample ") Client = client;} public async Task GetAspNetDocsIssues () {var response = await Client.GetAsync ("/ repos/aspnet/AspNetCore.Docs/issues?state=open&sort=created&direction=desc"); response.EnsureSuccessStatusCode (); var result = await response.Content .ReadAsync (); return result;}} public class GitHubIssue {public string url {get; set;} public int? Id {get; set;} public string node_id {get; set;}}
In the above code, the configuration is transferred to the typed client. The HttpClient object is exposed as a public property. You can define API-specific methods that expose HttpClient functionality. The GetAspNetDocsIssues method encapsulates the code needed to query and analyze the latest unresolved problems from the GitHub repository.
To register a typed client, use the common AddHttpClient extension method in Startup.ConfigureServices to specify the typed client class:
Services.AddHttpClient ()
Use DI to register the type client as a temporary client. You can insert or use typed clients directly:
Public class TypedClientModel: PageModel {private readonly GitHubService _ gitHubService; public IEnumerable LatestIssues {get; private set;} public bool HasIssue = > LatestIssues.Any (); public bool GetIssuesError {get; private set;} public TypedClientModel (GitHubService gitHubService) {_ gitHubService = gitHubService;} public async Task OnGet () {try {LatestIssues = await _ gitHubService.GetAspNetDocsIssues () } catch (HttpRequestException) {GetIssuesError = true; LatestIssues = Array.Empty ();} above is all the content of the article "how to use IHttpClientFactory to send HTTP requests by ASP.NET Core". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.