From 276f93912ac0e861349a67a28169760c6f7672f9 Mon Sep 17 00:00:00 2001 From: John Zabroski Date: Tue, 16 Apr 2019 10:41:33 -0400 Subject: [PATCH 1/3] Warning about incorrect lifecycle policy --- xml/System.Net.Http/HttpClient.xml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xml/System.Net.Http/HttpClient.xml b/xml/System.Net.Http/HttpClient.xml index 228db3c2597..0c806afe4fb 100644 --- a/xml/System.Net.Http/HttpClient.xml +++ b/xml/System.Net.Http/HttpClient.xml @@ -28,6 +28,10 @@ [!WARNING] + > is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. + The class instance acts as a session to send HTTP requests. An instance is a collection of settings applied to all requests executed by that instance. In addition, every instance uses its own connection pool, isolating its requests from requests executed by other instances. The also acts as a base class for more specific HTTP clients. An example would be a FacebookHttpClient providing additional methods specific to a Facebook web service (a GetFriends method, for instance). Derived classes should not override the virtual methods on the class. Instead, use a constructor overload that accepts to configure any pre- or post-request processing instead. @@ -56,7 +60,7 @@ 9. - is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly. +Below is an example using HttpClient correctly: ```csharp public class GoodController : ApiController @@ -2152,4 +2156,4 @@ public class GoodController : ApiController - \ No newline at end of file + From 2779740e727da1820c4158b505c55072ba88d27a Mon Sep 17 00:00:00 2001 From: Ron Petrusha Date: Tue, 16 Apr 2019 12:08:07 -0400 Subject: [PATCH 2/3] Update xml/System.Net.Http/HttpClient.xml Co-Authored-By: jzabroski --- xml/System.Net.Http/HttpClient.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml/System.Net.Http/HttpClient.xml b/xml/System.Net.Http/HttpClient.xml index 0c806afe4fb..533f78e5703 100644 --- a/xml/System.Net.Http/HttpClient.xml +++ b/xml/System.Net.Http/HttpClient.xml @@ -30,7 +30,7 @@ ## Remarks > [!WARNING] - > is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. + > is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in a . The class instance acts as a session to send HTTP requests. An instance is a collection of settings applied to all requests executed by that instance. In addition, every instance uses its own connection pool, isolating its requests from requests executed by other instances. From fad9ccadbbe39c037926d80ec0aa9b41aed85f6e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:42:59 -0700 Subject: [PATCH 3/3] Apply suggestions from code review --- xml/System.Net.Http/HttpClient.xml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xml/System.Net.Http/HttpClient.xml b/xml/System.Net.Http/HttpClient.xml index 533f78e5703..82a63185441 100644 --- a/xml/System.Net.Http/HttpClient.xml +++ b/xml/System.Net.Http/HttpClient.xml @@ -30,7 +30,11 @@ ## Remarks > [!WARNING] - > is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in a . + > - is intended to be instantiated once and re-used throughout the life of an application. Instantiating an class for every request will exhaust the number of sockets available under heavy loads and result in a . However, having long-lived instances can lead to stale DNS usage and non-functional connections. Use one of the following options to avoid these problems: +> +> - Use static instances, which are recycled on a regular basis. +> - Use to create the . +> - On .NET Core and .NET 5.0 and later, use static instances and set . The class instance acts as a session to send HTTP requests. An instance is a collection of settings applied to all requests executed by that instance. In addition, every instance uses its own connection pool, isolating its requests from requests executed by other instances. @@ -60,7 +64,7 @@ 9. -Below is an example using HttpClient correctly: +The following code snippet shows how to use : ```csharp public class GoodController : ApiController