-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Warning about incorrect lifecycle policy #2315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,10 @@ | |
<format type="text/markdown"><![CDATA[ | ||
|
||
## Remarks | ||
|
||
> [!WARNING] | ||
> <xref:System.Net.Http.HttpClient> 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. | ||
jzabroski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
The <xref:System.Net.Http.HttpClient> class instance acts as a session to send HTTP requests. An <xref:System.Net.Http.HttpClient> instance is a collection of settings applied to all requests executed by that instance. In addition, every <xref:System.Net.Http.HttpClient> instance uses its own connection pool, isolating its requests from requests executed by other <xref:System.Net.Http.HttpClient> instances. | ||
|
||
The <xref:System.Net.Http.HttpClient> 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 <xref:System.Net.Http.HttpMessageHandler> to configure any pre- or post-request processing instead. | ||
|
@@ -56,7 +60,7 @@ | |
|
||
9. <xref:System.Net.Http.HttpClient.SendAsync%2A> | ||
|
||
<xref:System.Net.Http.HttpClient> 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 | |
</Docs> | ||
</Member> | ||
</Members> | ||
</Type> | ||
</Type> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jzabroski. Unfortunately, this recommendation is not complete.
We need to warn users also about having long-existing
HttpClient
instances as that may lead to stale DNS usage. That can lead to non-functional connections.The recommendation should be either to:
HttpClient
instances as statics, which are recycled on regular basis, orHttpClientFactor
which works around this problem, orSystem.Net.Http.SocketsHttpHandler.PooledConnectionLifetime
.