diff --git a/src/libraries/Common/src/System/AppContextSwitchHelper.cs b/src/libraries/Common/src/System/AppContextSwitchHelper.cs
new file mode 100644
index 00000000000000..32d8d9b4445056
--- /dev/null
+++ b/src/libraries/Common/src/System/AppContextSwitchHelper.cs
@@ -0,0 +1,30 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Globalization;
+
+namespace System
+{
+ internal static class AppContextSwitchHelper
+ {
+ internal static bool GetBooleanConfig(string switchName, bool defaultValue) =>
+ AppContext.TryGetSwitch(switchName, out bool value) ? value : defaultValue;
+
+ internal static bool GetBooleanConfig(string switchName, string envVariable, bool defaultValue = false)
+ {
+ if (Environment.GetEnvironmentVariable(envVariable) is string str)
+ {
+ if (str == "1" || string.Equals(str, bool.TrueString, StringComparison.OrdinalIgnoreCase))
+ {
+ return true;
+ }
+ if (str == "1" || string.Equals(str, bool.FalseString, StringComparison.OrdinalIgnoreCase))
+ {
+ return false;
+ }
+ }
+
+ return GetBooleanConfig(switchName, defaultValue);
+ }
+ }
+}
diff --git a/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj b/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj
index 3dc678ce8442cf..3f45ed87f8165e 100644
--- a/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj
+++ b/src/libraries/System.Net.Quic/src/System.Net.Quic.csproj
@@ -29,6 +29,7 @@
+
diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs
index a4162d03a4afaf..ed20757c33947d 100644
--- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs
+++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs
@@ -74,7 +74,7 @@ private MsQuicApi(QUIC_API_TABLE* apiTable)
static MsQuicApi()
{
bool loaded = false;
- IntPtr msQuicHandle;
+ IntPtr msQuicHandle = IntPtr.Zero;
Version = default;
// MsQuic is using DualMode sockets and that will fail even for IPv4 if AF_INET6 is not available.
@@ -97,7 +97,10 @@ static MsQuicApi()
// support developers explicitly providing OpenSSL version of MsQuic.
// in the application directory, so we first check there and default
// to the Schannel version if not found.
- loaded = NativeLibrary.TryLoad(Path.Combine(AppContext.BaseDirectory, Interop.Libraries.MsQuic), out msQuicHandle);
+ if (AllowAppLocalMsQuic())
+ {
+ loaded = NativeLibrary.TryLoad(Path.Combine(AppContext.BaseDirectory, Interop.Libraries.MsQuic), out msQuicHandle);
+ }
if (!loaded)
{
@@ -275,4 +278,6 @@ private static bool IsTls13Disabled(bool isServer)
#endif
return false;
}
+
+ private static bool AllowAppLocalMsQuic() => AppContextSwitchHelper.GetBooleanConfig("System.Net.Quic.AppLocalMsQuic", "DOTNET_SYSTEM_NET_QUIC_APPLOCALMSQUIC");
}