Skip to content

Commit

Permalink
enhance invariant mode checks (#1917)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 committed Feb 11, 2023
1 parent 6db45d4 commit a4f18ca
Showing 1 changed file with 29 additions and 1 deletion.
Expand Up @@ -1812,7 +1812,35 @@ private bool TryOpen(TaskCompletionSource<DbConnectionInternal> retry, SqlConnec
// are not present. Throwing on open with a meaningful message helps identify the issue.
if (_cultureCheckState == CultureCheckState.Unknown)
{
_cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard;
// check if invariant state has been set by appcontext switch directly
if (AppContext.TryGetSwitch("System.Globalization.Invariant", out bool isEnabled) && isEnabled)
{
_cultureCheckState = CultureCheckState.Invariant;
}
else
{
// check if invariant state has been set through environment variables
string envValue = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");
if (string.Equals(envValue, bool.TrueString, StringComparison.OrdinalIgnoreCase) || string.Equals(envValue, "1", StringComparison.OrdinalIgnoreCase))
{
_cultureCheckState = CultureCheckState.Invariant;
}
else
{
// if it hasn't been manually set it could still apply if the os doesn't have
// icu libs installed or is a native binary with icu support trimmed away
// netcore 3.1 to net5 do not throw in attempting to create en-us in inariant mode
// net6 and greater will throw so catch and infer invariant mode from the exception
try
{
_cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard;
}
catch (CultureNotFoundException)
{
_cultureCheckState = CultureCheckState.Invariant;
}
}
}
}
if (_cultureCheckState == CultureCheckState.Invariant)
{
Expand Down

0 comments on commit a4f18ca

Please sign in to comment.