Skip to content

Commit

Permalink
Hide functionality behind appctx switch
Browse files Browse the repository at this point in the history
  • Loading branch information
rzikm committed Jun 14, 2024
1 parent 24f2e9f commit 6b967a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/libraries/Common/src/System/AppContextSwitchHelper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions src/libraries/System.Net.Quic/src/System.Net.Quic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Compile Include="System\Net\Quic\**\*.cs" Exclude="System\Net\Quic\*.Unsupported.cs"/>
<!-- System.Net common -->
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs" Link="Common\DisableRuntimeMarshalling.cs" />
<Compile Include="$(CommonPath)System\AppContextSwitchHelper.cs" Link="Common\System\AppContextSwitchHelper.cs" />
<Compile Include="$(CommonPath)System\Net\SafeHandleCache.cs" Link="Common\System\Net\SafeHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="Common\System\Net\MultiArrayBuffer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
{
Expand Down Expand Up @@ -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");
}

0 comments on commit 6b967a0

Please sign in to comment.