public partial class HttpClientHandler : HttpMessageHandler
{
// This partial implementation contains members common to all HttpClientHandler implementations.
- private const string ManagedHandlerSettingName = "COMPlus_UseManagedHttpClientHandler";
- private const string AppCtxManagedHandlerSettingName = "System.Net.Http.UseManagedHttpClientHandler";
+ private const string ManagedHandlerEnvironmentVariableSettingName = "DOTNET_SYSTEM_NET_HTTP_USEMANAGEDHTTPCLIENTHANDLER";
+ private const string ManagedHandlerAppCtxSettingName = "System.Net.Http.UseManagedHttpClientHandler";
private static LocalDataStoreSlot s_useManagedHandlerSlot;
{
get
{
- // Check the environment variable to see if it's been set to true. If it has, use the managed handler.
- if (Environment.GetEnvironmentVariable(ManagedHandlerSettingName) == "true")
+ // First check for the AppContext switch, giving it priority over over the environment variable.
+ if (AppContext.TryGetSwitch(ManagedHandlerAppCtxSettingName, out bool isManagedEnabled))
{
- return true;
+ return isManagedEnabled;
}
- if (AppContext.TryGetSwitch(AppCtxManagedHandlerSettingName, out bool isManagedEnabled) && isManagedEnabled)
+ // AppContext switch wasn't used. Check the environment variable to see if it's been set to true.
+ string envVar = Environment.GetEnvironmentVariable(ManagedHandlerEnvironmentVariableSettingName);
+ if (envVar != null && (envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1")))
{
return true;
}
+ // TODO #23166: Remove the following TLS check assuming the type is exposed publicly. If it's not,
+ // re-evaluate the priority ordering of this with regards to the AppContext and environment settings.
+
// Then check whether a thread local has been set with the same name.
// If it's been set to a Boolean true, also use the managed handler.
LocalDataStoreSlot slot = LazyInitializer.EnsureInitialized(ref s_useManagedHandlerSlot, () =>
{
- LocalDataStoreSlot local = Thread.GetNamedDataSlot(ManagedHandlerSettingName);
+ LocalDataStoreSlot local = Thread.GetNamedDataSlot(ManagedHandlerEnvironmentVariableSettingName);
if (local == null)
{
try
{
- local = Thread.AllocateNamedDataSlot(ManagedHandlerSettingName);
+ local = Thread.AllocateNamedDataSlot(ManagedHandlerEnvironmentVariableSettingName);
}
catch (ArgumentException)
{
- local = Thread.GetNamedDataSlot(ManagedHandlerSettingName);
+ local = Thread.GetNamedDataSlot(ManagedHandlerEnvironmentVariableSettingName);
}
}
return local;
#### Enabling the ManagedHandler
-The shipping version of HttpClientHandler is a wrapper for WinHTTP on Windows and libcurl on Unix. This directory contains a managed implementation that's still under development and that's not intended for production use. By default it's disabled, and the WinHTTP/libcurl versions will be used. However, by setting the "COMPlus_UseManagedHttpClientHandler" environment variable to "true", this managed implementation will be used.
\ No newline at end of file
+The shipping version of HttpClientHandler is a wrapper for WinHTTP on Windows and libcurl on Unix. This directory contains a managed implementation that's still under development and that's not intended for production use. By default it's disabled, and the WinHTTP/libcurl versions will be used. However, by setting the "DOTNET_SYSTEM_NET_HTTP_USEMANAGEDHTTPCLIENTHANDLER" environment variable to "true" or "1", this managed implementation will be used. It can also be enabled via the "System.Net.Http.UseManagedHttpClientHandler" AppContext setting.