Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the HttpClientFactory in the .NET Core development log

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, Xiaobian will bring you about how to understand HttpClientFactory in the. NET Core development log. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

The HttpClient class is often used when you need to send an HTTP request to a particular URL address and get a response. This class contains a number of useful methods that satisfy most requirements. However, if it is used improperly, unexpected things may occur.

You'RE USING HTTPCLIENT WRONG AND IT's destroying YOUR SOFTWARE.

The reason for this is a seemingly correct code:

using(var client = new HttpClient())

Resources occupied by objects should be guaranteed to be released in time, but this is wrong for network connections.

There are two reasons: network connections take a certain amount of time, frequently opening and closing connections, performance will be affected; moreover, opening a network connection will consume the underlying socket resources, but when HttpClient calls its own Dispose method, it cannot immediately release the resources, which means that your program may generate unexpected exceptions due to the exhaustion of connection resources.

So a better solution is to extend the life of the HttpClient object, such as creating a static object for it:

private static HttpClient Client = new HttpClient();

But from a programmer's point of view, such code may not be elegant enough.

So a new HttpClientFactory class was introduced in. NET Core 2.1.

Its usage is very simple, the first is to register it Ioc:

public void ConfigureServices(IServiceCollection services){ services.AddHttpClient(); services.AddMvc();}

Then create an HttpClient object through IHttpClientFactory, and then do the same as before, but don't worry about releasing its internal resources:

At first glance, you may not understand what the AddHttpClient method has to do with IHttpClientFactory, but you can see it clearly after looking up its source code:

Its internals bind the DefaultHttpClientFactory class for the IHttpClientFactory interface.

Take a look at the key CreateClient method in the IHttpClientFactory interface:

HttpClient is no longer created as a simple new HttpClient(), but two parameters are passed: HttpMessageHandler and bool disposeHandler. A value of false for the disposeHandler parameter indicates that the inner handler object is to be reused. The handler parameter can be seen from the code in the previous sentence that it is taken from a dictionary with the name as the key value, and because the DefaultHttpClientFactory class is registered through the TryAddSingleton method, it means that it is a single example, so this internal dictionary is unique, and the ActiveHandlerTrackingEntry object corresponding to each key value is also unique, and the object contains the handler inside.

The next line of code StartHandlerEntryTimer(entry); turns on expiration timing for ActiveHandlerTrackingEntry objects. The default expiration time is 2 minutes.

First pass the ActiveHandlerTrackingEntry object into the new ExpiredHandlerTrackingEntry object.

public ExpiredHandlerTrackingEntry(ActiveHandlerTrackingEntry other){ Name = other.Name; _livenessTracker = new WeakReference(other.Handler); InnerHandler = other.Handler.InnerHandler;}

Within its constructor, handler objects are associated by weak references that do not affect their release by GC.

The newly created ExpiredHandlerTrackingEntry object is then placed in a dedicated queue.

Finally, the cleaning operation starts, and the timer interval is set to once every 10 seconds.

The core of the above method is to judge whether the handler object has been GC, and if so, release its internal resources, i.e. network connections.

Going back to the original code that created HttpClient, you will notice that no name parameter value was passed. This is an extension method that benefits from the HttpClientFactoryExtensions class.

When passing in the name parameter value, you can also pass in some configuration information:

The above is how to understand the HttpClientFactory in the. NET Core development log shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report