How to use HttpClient in ASP.NET Core 2.0 API

I am working on an ASP.NET Core 2.0 API, and my API needs to make calls to another, third-party, REST API to load and extract files, get file lists and status information. I will host the API on Azure and plan to deploy Blue-Green between my intermediate and production slots.

It seems that the general consensus regarding best practice is to configure the SingleClate HTTPClient instance through DI registration in Startup.cs -> ConfigureSerrvices to improve performance and avoid socket errors that may occur if I am new to establish an HTTPClient connection with each use using the Using statement. This is noted in the following links:

https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx#Anchor_5

http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/

But if I do this, then I may run into a problem when the Singleton instance does not see any DNS changes when I deploy Blue-Green in Azure. This is noted in the following links:

http://byterot.blogspot.co.uk/2016/07/singleton-httpclient-dns.html

https://github.com/dotnet/corefx/issues/11224

http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/

, HTTPClient, ConnectionLeaseTimeout ServicePoint , , DNS. RestClient nuget (Easy.Common by Nima), ConnectionLeaseTimeout, DNS.

, , ASP.NET Core 2.0 ServicePoint, ASP.Net Core 2.0.

- , HttpClient API ASP.NET Core 2.0, Azure? Blue-Green. , Using ?

, .

+4
2

, , , , ConnectionLeaseTimeout - , , , . , , , Connection: Close , . . , . Flurl, .

, , DNS . , , ( 100 ). HttpClient .

, . , , , DefaultRequestHeaders, , , . , . ( , 1000 , 1000 , .) , DNS- , Connection: Close HttpClient . , , , .

+3

, , , HttpClient, ( ).

, , -, . HttpClient , , HttpClient, , -, HttpClient.

, , using HttpClient, . , .

+3

Source: https://habr.com/ru/post/1691635/


All Articles