Fix ManagedHandler env var / AppContext handling (dotnet/corefx#26763)
authorStephen Toub <stoub@microsoft.com>
Fri, 2 Feb 2018 03:04:16 +0000 (22:04 -0500)
committerGitHub <noreply@github.com>
Fri, 2 Feb 2018 03:04:16 +0000 (22:04 -0500)
* Fix ManagedHandler env var / AppContext handling

* Update two names

* Update README.md

Commit migrated from https://github.com/dotnet/corefx/commit/473a4cf05c28f95d8a93b826647f755dbdec8728

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs
src/libraries/System.Net.Http/src/System/Net/Http/Managed/README.md
src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTestBase.cs
src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs
src/libraries/System.Net.WebSockets.WebSocketProtocol/tests/WebSocketProtocolTests.cs

index 119644a..00c557f 100644 (file)
@@ -12,8 +12,8 @@ namespace System.Net.Http
     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;
 
@@ -21,31 +21,36 @@ namespace System.Net.Http
         {
             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;
index c7d6a58..9b7cf65 100644 (file)
@@ -1,3 +1,3 @@
 #### 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.
index c8220d9..6790eca 100644 (file)
@@ -9,7 +9,7 @@ namespace System.Net.Http.Functional.Tests
 {
     public abstract class HttpClientTestBase : RemoteExecutorTestBase
     {
-        private const string ManagedHandlerEnvVar = "COMPlus_UseManagedHttpClientHandler";
+        private const string ManagedHandlerEnvVar = "DOTNET_SYSTEM_NET_HTTP_USEMANAGEDHTTPCLIENTHANDLER";
         private static readonly LocalDataStoreSlot s_managedHandlerSlot;
 
         static HttpClientTestBase()
index a9605b2..dfbfb28 100644 (file)
@@ -69,7 +69,7 @@ namespace System.Net.WebSockets
 
         private sealed class DirectManagedHttpClientHandler : HttpClientHandler
         {
-            private const string ManagedHandlerEnvVar = "COMPlus_UseManagedHttpClientHandler";
+            private const string ManagedHandlerEnvVar = "DOTNET_SYSTEM_NET_HTTP_USEMANAGEDHTTPCLIENTHANDLER";
             private static readonly LocalDataStoreSlot s_managedHandlerSlot = GetSlot();
             private static readonly object s_true = true;
 
index 2b5964e..597df59 100644 (file)
@@ -125,7 +125,7 @@ namespace System.Net.WebSockets.Tests
 
         private sealed class DirectManagedHttpClientHandler : HttpClientHandler
         {
-            private const string ManagedHandlerEnvVar = "COMPlus_UseManagedHttpClientHandler";
+            private const string ManagedHandlerEnvVar = "DOTNET_SYSTEM_NET_HTTP_USEMANAGEDHTTPCLIENTHANDLER";
             private static readonly LocalDataStoreSlot s_managedHandlerSlot = GetSlot();
             private static readonly object s_true = true;