-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
"Managing Connections" mistakenly suggests that ServicePointManager is used globally #9295
Comments
See this comment about the implementation: https://github.com/dotnet/corefx/issues/11224#issuecomment-289141149 Relevant portion:
|
@karelz is there any more we should add to this doc besides
|
Tagging @davidsh as well. |
In general, on .NET Core, ServicePointManager is not used at all. ServicePoint/ServicePointManager are considered legacy and are only stubbed out in .NET Core. Very little is used by any of the networking stack in .NET Core, in only a few places referenced from HttpWebRequest (also legacy) in an attempt to respect some globally-set settings. ServicePoint doesn’t track anything about current connections used by HttpClient (or HttpWebRequest, that layers on top of it) and is hardcoded to return 0: |
@erikness This comment saved me hours of time trying to figure out why my HttpClient wouldn't obey the Thank you. |
This issue has been closed as part of the issue backlog grooming process outlined in #22351. That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the |
Issue description
The "Managing Connections" doc describes how to use ServicePoint and ServicePointManager objects to manage TCP connections. These objects don't do anything themselves; they mostly hold configuration. The issue is that these are not used everywhere in .NET: some ways of sending network requests obey the restrictions you set in ServicePoint/ServicePointManager, but some other ways keep their configuration elsewhere.
Let me get specific.
The standard way of sending an HTTP request involves first creating an HttpClient object. You can optionally inject an HttpMessageListener object into HttpClient which controls much of HttpClient's behavior. If you don't specify one, HttpClient will use an instance of the class HttpClientHandler (which implements HttpMessageListener).
Microsoft provides an alternative handler called WinHttpHandler as well. There are some other handlers that I haven't explored I believe and developers are free to implement their own.
HttpClientHandler uses ServicePointManager to manage its TCP connections. But WinHttpHandler uses the windows library WinHTTP to manage its TCP connections. This means that if you change the properties of ServicePointManager or any specific ServicePoint object and then use WinHttpHandler to send HTTP requests, your changes won't have any affect.
For example, to set the maximum number of connections allowed per endpoint:
The issue with this doc is that it speaks about ServicePointManager as though it manages TCP connections no matter what, when in fact it only manages TCP connections for some handler classes. I don't actually know the complete description of what's going on here. It should at least mention that if you're using WinHttpHandler or SocketsHttpHandler that you should not use ServicePointManager.
Reproduction
Here's a sketch of a repro (let me know if it doesn't work and I can get you a real version):
Related info
You might object that the doc is scoped correctly because WinHttpHandler is not included with .NET Framework by default (you have to get it from a NuGet package). My feeling from reading StackOverflow links is that WinHttpHandler is the most modern handler for .NET Framework projects; for example, it's the only one that supports HTTP/2. In addition, in .NET Core, HttpClientHandler uses WinHttpHandler behind the scenes.
In addition, .NET introduces a new handler called SocketsHttpHandler. I believe it manages its own connections so I'd bet it doesn't use ServicePoint's either.
Target framework
Check the .NET target framework(s) being used, and include the version number(s).
About VS info
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: