21 private const int DefaultAspPersistentConnectionLimit = 10;
23 internal static readonly
string SpecialConnectGroupName =
"/.NET/NetClasses/HttpWebRequest/CONNECT__Group$$/";
25 internal static readonly TimerThread.Callback s_IdleServicePointTimeoutDelegate = IdleServicePointTimeoutCallback;
29 private static volatile TimerThread.Queue s_ServicePointIdlingQueue = TimerThread.GetOrCreateQueue(100000);
31 private static int s_MaxServicePoints = 0;
33 private static volatile CertPolicyValidationCallback s_CertPolicyValidationCallback =
new CertPolicyValidationCallback();
35 private static volatile ServerCertValidationCallback s_ServerCertValidationCallback =
null;
39 private static bool s_reusePort;
41 private static bool? s_reusePortSupported =
null;
43 private static bool s_disableStrongCrypto;
45 private static bool s_disableSendAuxRecord;
47 private static bool s_disableSystemDefaultTlsVersions;
51 private static bool s_disableCertificateEKUs;
53 private static volatile Hashtable s_ConfigTable =
null;
55 private static volatile int s_ConnectionLimit = PersistentConnectionLimit;
57 internal static volatile bool s_UseTcpKeepAlive =
false;
59 internal static volatile int s_TcpKeepAliveTime;
61 internal static volatile int s_TcpKeepAliveInterval;
63 private static volatile bool s_UserChangedLimit;
65 private static object s_configurationLoadedLock =
new object();
67 private static volatile bool s_configurationLoaded =
false;
69 private const string RegistryGlobalStrongCryptoName =
"SchUseStrongCrypto";
71 private const string RegistryGlobalReusePortName =
"HWRPortReuseOnSocketBind";
73 private const string RegistryGlobalSendAuxRecordName =
"SchSendAuxRecord";
75 private const string RegistryLocalSendAuxRecordName =
"System.Net.ServicePointManager.SchSendAuxRecord";
77 private const string RegistryGlobalSystemDefaultTlsVersionsName =
"SystemDefaultTlsVersions";
79 private const string RegistryLocalSystemDefaultTlsVersionsName =
"System.Net.ServicePointManager.SystemDefaultTlsVersions";
81 private const string RegistryLocalSecureProtocolName =
"System.Net.ServicePointManager.SecurityProtocol";
83 private const string RegistryGlobalRequireCertificateEKUs =
"RequireCertificateEKUs";
85 private const string RegistryLocalRequireCertificateEKUs =
"System.Net.ServicePointManager.RequireCertificateEKUs";
87 private static int InternalConnectionLimit
91 if (s_ConfigTable ==
null)
93 s_ConfigTable = ConfigTable;
95 return s_ConnectionLimit;
99 if (s_ConfigTable ==
null)
101 s_ConfigTable = ConfigTable;
103 s_UserChangedLimit =
true;
104 s_ConnectionLimit = value;
108 private static int PersistentConnectionLimit
112 if (ComNetOS.IsAspNetServer)
124 if (s_ConfigTable ==
null)
126 lock (s_ServicePointTable)
128 if (s_ConfigTable ==
null)
130 ConnectionManagementSectionInternal section = ConnectionManagementSectionInternal.GetSection();
134 hashtable = section.ConnectionManagement;
136 if (hashtable ==
null)
142 int num = (int)hashtable[
"*"];
145 num = PersistentConnectionLimit;
147 s_ConnectionLimit = num;
149 s_ConfigTable = hashtable;
153 return s_ConfigTable;
157 internal static TimerThread.Callback IdleServicePointTimeoutDelegate => s_IdleServicePointTimeoutDelegate;
166 EnsureConfigurationLoaded();
167 return s_SecurityProtocolType;
171 EnsureConfigurationLoaded();
172 ValidateSecurityProtocol(value);
173 s_SecurityProtocolType = value;
177 internal static bool DisableStrongCrypto
181 EnsureConfigurationLoaded();
182 return s_disableStrongCrypto;
186 internal static bool DisableSystemDefaultTlsVersions
190 EnsureConfigurationLoaded();
191 return s_disableSystemDefaultTlsVersions;
195 internal static bool DisableSendAuxRecord
199 EnsureConfigurationLoaded();
200 return s_disableSendAuxRecord;
204 internal static bool DisableCertificateEKUs
208 EnsureConfigurationLoaded();
209 return s_disableCertificateEKUs;
217 EnsureConfigurationLoaded();
218 return s_defaultSslProtocols;
230 return s_MaxServicePoints;
234 ExceptionHelper.WebPermissionUnrestricted.Demand();
235 if (!ValidationHelper.ValidateRange(value, 0,
int.MaxValue))
239 s_MaxServicePoints = value;
251 return InternalConnectionLimit;
255 ExceptionHelper.WebPermissionUnrestricted.Demand();
258 InternalConnectionLimit = value;
273 return s_ServicePointIdlingQueue.Duration;
277 ExceptionHelper.WebPermissionUnrestricted.Demand();
278 if (!ValidationHelper.ValidateRange(value, -1,
int.MaxValue))
282 if (s_ServicePointIdlingQueue.Duration != value)
284 s_ServicePointIdlingQueue = TimerThread.GetOrCreateQueue(value);
296 return SettingsSectionInternal.Section.UseNagleAlgorithm;
300 SettingsSectionInternal.Section.UseNagleAlgorithm = value;
311 return SettingsSectionInternal.Section.Expect100Continue;
315 SettingsSectionInternal.Section.Expect100Continue = value;
326 return SettingsSectionInternal.Section.EnableDnsRoundRobin;
330 SettingsSectionInternal.Section.EnableDnsRoundRobin = value;
340 return SettingsSectionInternal.Section.DnsRefreshTimeout;
346 SettingsSectionInternal.Section.DnsRefreshTimeout = -1;
350 SettingsSectionInternal.Section.DnsRefreshTimeout = value;
357 [Obsolete(
"CertificatePolicy is obsoleted for this type, please use ServerCertificateValidationCallback instead. http://go.microsoft.com/fwlink/?linkid=14202")]
362 return GetLegacyCertificatePolicy();
366 ExceptionHelper.UnmanagedPermission.Demand();
367 s_CertPolicyValidationCallback =
new CertPolicyValidationCallback(value);
371 internal static CertPolicyValidationCallback CertPolicyValidationCallback => s_CertPolicyValidationCallback;
379 if (s_ServerCertValidationCallback ==
null)
383 return s_ServerCertValidationCallback.ValidationCallback;
387 ExceptionHelper.InfrastructurePermission.Demand();
390 s_ServerCertValidationCallback =
null;
394 s_ServerCertValidationCallback =
new ServerCertValidationCallback(value);
399 internal static ServerCertValidationCallback ServerCertValidationCallback => s_ServerCertValidationCallback;
415 internal static bool? ReusePortSupported
419 return s_reusePortSupported;
423 s_reusePortSupported = value;
434 return SettingsSectionInternal.Section.CheckCertificateRevocationList;
438 ExceptionHelper.UnmanagedPermission.Demand();
439 SettingsSectionInternal.Section.CheckCertificateRevocationList = value;
447 internal static bool CheckCertificateName => SettingsSectionInternal.Section.CheckCertificateName;
449 [Conditional(
"DEBUG")]
450 internal static void DebugMembers(
int requestHash)
468 private static void IdleServicePointTimeoutCallback(TimerThread.Timer timer,
int timeNoticed,
object context)
473 Logging.PrintInfo(Logging.Web, SR.GetString(
"net_log_closed_idle",
"ServicePoint", servicePoint.GetHashCode()));
475 lock (s_ServicePointTable)
477 s_ServicePointTable.
Remove(servicePoint.LookupString);
479 servicePoint.ReleaseAllConnectionGroups();
482 private ServicePointManager()
489 if ((value & ~securityProtocolType) != 0)
491 throw new NotSupportedException(SR.GetString(
"net_securityprotocolnotsupported"));
495 internal static ICertificatePolicy GetLegacyCertificatePolicy()
497 if (s_CertPolicyValidationCallback ==
null)
501 return s_CertPolicyValidationCallback.CertificatePolicy;
504 internal static string MakeQueryString(Uri address)
506 if (address.IsDefaultPort)
508 return address.Scheme +
"://" + address.DnsSafeHost;
510 return address.Scheme +
"://" + address.DnsSafeHost +
":" + address.Port.ToString();
513 internal static string MakeQueryString(Uri address1,
bool isProxy)
517 return MakeQueryString(address1) +
"://proxy";
519 return MakeQueryString(address1);
541 Uri address =
new Uri(uriString);
554 HttpAbortDelegate abortDelegate =
null;
557 return FindServicePoint(address, proxy, out chain, ref abortDelegate, ref abortState);
566 bool isProxyServicePoint =
false;
571 IAutoWebProxy autoWebProxy = proxy as IAutoWebProxy;
572 if (autoWebProxy !=
null)
574 chain = autoWebProxy.GetProxies(address);
575 abortDelegate = chain.HttpAbortDelegate;
584 chain.Enumerator.MoveNext();
585 uri = chain.Enumerator.Current;
589 abortDelegate =
null;
599 isProxyServicePoint =
true;
602 return FindServicePointHelper(address, isProxyServicePoint);
607 if (!chain.Enumerator.MoveNext())
611 Uri current = chain.Enumerator.Current;
612 return FindServicePointHelper((current ==
null) ? chain.Destination : current, current !=
null);
615 private static ServicePoint FindServicePointHelper(Uri address,
bool isProxyServicePoint)
617 if (isProxyServicePoint && address.Scheme != Uri.UriSchemeHttp)
619 Exception ex =
new NotSupportedException(SR.GetString(
"net_proxyschemenotsupported", address.Scheme));
622 string text = MakeQueryString(address, isProxyServicePoint);
623 ServicePoint servicePoint =
null;
624 lock (s_ServicePointTable)
626 WeakReference weakReference = s_ServicePointTable[text] as WeakReference;
627 if (weakReference !=
null)
629 servicePoint = (ServicePoint)weakReference.Target;
631 if (servicePoint !=
null)
635 if (s_MaxServicePoints > 0 && s_ServicePointTable.
Count >= s_MaxServicePoints)
637 Exception ex2 =
new InvalidOperationException(SR.GetString(
"net_maxsrvpoints"));
640 int defaultConnectionLimit = InternalConnectionLimit;
641 string key = MakeQueryString(address);
642 bool userChangedLimit = s_UserChangedLimit;
645 defaultConnectionLimit = (int)ConfigTable[key];
646 userChangedLimit =
true;
648 servicePoint =
new ServicePoint(address, s_ServicePointIdlingQueue, defaultConnectionLimit, text, userChangedLimit, isProxyServicePoint);
649 weakReference =
new WeakReference(servicePoint);
650 s_ServicePointTable[text] = weakReference;
659 throw new ArgumentNullException(
"address");
662 bool proxyServicePoint =
false;
664 ServicePoint servicePoint =
null;
665 lock (s_ServicePointTable)
667 WeakReference weakReference = s_ServicePointTable[text] as WeakReference;
668 if (weakReference !=
null)
670 servicePoint = (ServicePoint)weakReference.Target;
672 if (servicePoint !=
null)
676 if (s_MaxServicePoints > 0 && s_ServicePointTable.
Count >= s_MaxServicePoints)
678 Exception ex =
new InvalidOperationException(SR.GetString(
"net_maxsrvpoints"));
681 int defaultConnectionLimit = InternalConnectionLimit;
682 bool userChangedLimit = s_UserChangedLimit;
686 defaultConnectionLimit = (int)ConfigTable[key];
687 userChangedLimit =
true;
689 servicePoint =
new ServicePoint(host, port, s_ServicePointIdlingQueue, defaultConnectionLimit, text, userChangedLimit, proxyServicePoint);
690 weakReference =
new WeakReference(servicePoint);
691 s_ServicePointTable[text] = weakReference;
696 [FriendAccessAllowed]
697 internal static void CloseConnectionGroups(
string connectionGroupName)
699 ServicePoint servicePoint =
null;
700 lock (s_ServicePointTable)
704 WeakReference weakReference = item.Value as WeakReference;
705 if (weakReference !=
null)
707 ((ServicePoint)weakReference.Target)?.CloseConnectionGroupInternal(connectionGroupName);
718 public static void SetTcpKeepAlive(
bool enabled,
int keepAliveTime,
int keepAliveInterval)
722 s_UseTcpKeepAlive =
true;
723 if (keepAliveTime <= 0)
727 if (keepAliveInterval <= 0)
731 s_TcpKeepAliveTime = keepAliveTime;
732 s_TcpKeepAliveInterval = keepAliveInterval;
736 s_UseTcpKeepAlive =
false;
737 s_TcpKeepAliveTime = 0;
738 s_TcpKeepAliveInterval = 0;
742 private static void LoadConfiguration()
744 s_reusePort = TryInitialize(LoadReusePortConfiguration, fallbackDefault:
false);
745 s_disableStrongCrypto = TryInitialize(LoadDisableStrongCryptoConfiguration, fallbackDefault:
true);
746 s_disableSendAuxRecord = TryInitialize(LoadDisableSendAuxRecordConfiguration, fallbackDefault:
false);
747 s_disableSystemDefaultTlsVersions = TryInitialize(LoadDisableSystemDefaultTlsVersionsConfiguration, fallbackDefault:
true);
748 s_disableCertificateEKUs = TryInitialize(LoadDisableCertificateEKUsConfiguration, fallbackDefault:
false);
749 s_defaultSslProtocols = TryInitialize(LoadSecureProtocolConfiguration,
SslProtocols.Default);
753 private static bool LoadDisableStrongCryptoConfiguration(
bool disable)
758 num = RegistryConfiguration.GlobalConfigReadInt(
"SchUseStrongCrypto", 0);
759 disable = (num != 1);
763 num = RegistryConfiguration.GlobalConfigReadInt(
"SchUseStrongCrypto", 1);
764 disable = (num == 0);
769 private static bool LoadDisableSendAuxRecordConfiguration(
bool disable)
771 if (RegistryConfiguration.AppConfigReadInt(
"System.Net.ServicePointManager.SchSendAuxRecord", 1) == 0)
775 if (RegistryConfiguration.GlobalConfigReadInt(
"SchSendAuxRecord", 1) == 0)
782 private static bool LoadDisableSystemDefaultTlsVersionsConfiguration(
bool disable)
786 int num = RegistryConfiguration.GlobalConfigReadInt(
"SystemDefaultTlsVersions", 0);
787 disable = (num != 1);
791 int num2 = RegistryConfiguration.GlobalConfigReadInt(
"SystemDefaultTlsVersions", 1);
792 disable = (num2 == 0);
796 int num3 = RegistryConfiguration.AppConfigReadInt(
"System.Net.ServicePointManager.SystemDefaultTlsVersions", 1);
797 disable = (num3 != 1);
805 if (!s_disableStrongCrypto || !s_disableSystemDefaultTlsVersions)
807 string value = RegistryConfiguration.AppConfigReadString(
"System.Net.ServicePointManager.SecurityProtocol",
null);
810 ValidateSecurityProtocol(result);
817 private static bool LoadReusePortConfiguration(
bool reusePortInternal)
820 num = RegistryConfiguration.GlobalConfigReadInt(
"HWRPortReuseOnSocketBind", 0);
825 Logging.PrintInfo(Logging.Web, typeof(ServicePointManager), SR.GetString(
"net_log_set_socketoption_reuseport_default_on"));
827 reusePortInternal =
true;
829 return reusePortInternal;
832 private static bool LoadDisableCertificateEKUsConfiguration(
bool disable)
838 if (RegistryConfiguration.AppConfigReadInt(
"System.Net.ServicePointManager.RequireCertificateEKUs", 1) == 0)
842 if (RegistryConfiguration.GlobalConfigReadInt(
"RequireCertificateEKUs", 1) == 0)
849 private static void EnsureConfigurationLoaded()
851 if (!s_configurationLoaded)
853 lock (s_configurationLoadedLock)
855 if (!s_configurationLoaded)
858 s_configurationLoaded =
true;
864 private static T TryInitialize<T>(Func<T, T> loadConfiguration, T fallbackDefault)
868 return loadConfiguration(fallbackDefault);
870 catch (Exception exception)
872 if (NclUtilities.IsFatal(exception))
876 return fallbackDefault;
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
static bool ReusePort
Setting this property value to true causes all outbound TCP connections from HttpWebRequest to use th...
static RemoteCertificateValidationCallback ServerCertificateValidationCallback
Gets or sets the callback to validate a server certificate.
static bool EnableDnsRoundRobin
Gets or sets a value that indicates whether a Domain Name Service (DNS) resolution rotates among the ...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static bool CheckCertificateRevocationList
Gets or sets a T:System.Boolean value that indicates whether the certificate is checked against the c...
static void MemoryBarrier()
Synchronizes memory access as follows: The processor executing the current thread cannot reorder inst...
The exception that is thrown when the execution stack overflows because it contains too many nested m...
EncryptionPolicy
The EncryptionPolicy to use.
static bool Expect100Continue
Gets or sets a T:System.Boolean value that determines whether 100-Continue behavior is used.
Manages the collection of T:System.Net.ServicePoint objects.
Uri GetProxy(Uri destination)
Returns the URI of a proxy.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
bool IsLoopback
Gets whether the specified T:System.Uri references the local host.
delegate bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
Verifies the remote Secure Sockets Layer (SSL) certificate used for authentication.
Validates a server certificate.
bool IsBypassed(Uri host)
Indicates that the proxy should not be used for the specified host.
virtual bool ContainsKey(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
No authentication is allowed. A client requesting an T:System.Net.HttpListener object with this flag ...
static int DnsRefreshTimeout
Gets or sets a value that indicates how long a Domain Name Service (DNS) resolution is considered val...
Represents a weak reference, which references an object while still allowing that object to be reclai...
static int MaxServicePoints
Gets or sets the maximum number of T:System.Net.ServicePoint objects to maintain at any time.
SslProtocols
Defines the possible versions of T:System.Security.Authentication.SslProtocols.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
The exception that is thrown when there is not enough memory to continue the execution of a program.
static void SetTcpKeepAlive(bool enabled, int keepAliveTime, int keepAliveInterval)
Enables or disables the keep-alive option on a TCP connection.
static int DefaultConnectionLimit
Gets or sets the maximum number of concurrent connections allowed by a T:System.Net....
static int MaxServicePointIdleTime
Gets or sets the maximum idle time of a T:System.Net.ServicePoint object.
WebExceptionStatus
Defines status codes for the T:System.Net.WebException class.
const int DefaultNonPersistentConnectionLimit
The default number of non-persistent connections (4) allowed on a T:System.Net.ServicePoint object co...
SecurityProtocolType
Specifies the security protocols that are supported by the Schannel security package.
static SecurityProtocolType SecurityProtocol
Gets or sets the security protocol used by the T:System.Net.ServicePoint objects managed by the T:Sys...
Attribute can be applied to an enumeration.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
static ICertificatePolicy CertificatePolicy
Gets or sets policy for server certificates.
static bool UseNagleAlgorithm
Determines whether the Nagle algorithm is used by the service points managed by this T:System....
Provides the base interface for implementation of proxy access for the T:System.Net....
static ServicePoint FindServicePoint(Uri address)
Finds an existing T:System.Net.ServicePoint object or creates a new T:System.Net.ServicePoint object ...
Provides information about a specific culture (called a locale for unmanaged code development)....
const int DefaultPersistentConnectionLimit
The default number of persistent connections (2) allowed on a T:System.Net.ServicePoint object connec...
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
The exception that is thrown when a call is made to the M:System.Threading.Thread....
static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
Finds an existing T:System.Net.ServicePoint object or creates a new T:System.Net.ServicePoint object ...
Defines a dictionary key/value pair that can be set or retrieved.
static ServicePoint FindServicePoint(string uriString, IWebProxy proxy)
Finds an existing T:System.Net.ServicePoint object or creates a new T:System.Net.ServicePoint object ...
Provides connection management for HTTP connections.
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Creates and controls a thread, sets its priority, and gets its status.