Skip to content

Commit a4f18ca

Browse files
authored
enhance invariant mode checks (#1917)
1 parent 6db45d4 commit a4f18ca

File tree

1 file changed

+29
-1
lines changed
  • src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient

1 file changed

+29
-1
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,35 @@ private bool TryOpen(TaskCompletionSource<DbConnectionInternal> retry, SqlConnec
18121812
// are not present. Throwing on open with a meaningful message helps identify the issue.
18131813
if (_cultureCheckState == CultureCheckState.Unknown)
18141814
{
1815-
_cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard;
1815+
// check if invariant state has been set by appcontext switch directly
1816+
if (AppContext.TryGetSwitch("System.Globalization.Invariant", out bool isEnabled) && isEnabled)
1817+
{
1818+
_cultureCheckState = CultureCheckState.Invariant;
1819+
}
1820+
else
1821+
{
1822+
// check if invariant state has been set through environment variables
1823+
string envValue = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");
1824+
if (string.Equals(envValue, bool.TrueString, StringComparison.OrdinalIgnoreCase) || string.Equals(envValue, "1", StringComparison.OrdinalIgnoreCase))
1825+
{
1826+
_cultureCheckState = CultureCheckState.Invariant;
1827+
}
1828+
else
1829+
{
1830+
// if it hasn't been manually set it could still apply if the os doesn't have
1831+
// icu libs installed or is a native binary with icu support trimmed away
1832+
// netcore 3.1 to net5 do not throw in attempting to create en-us in inariant mode
1833+
// net6 and greater will throw so catch and infer invariant mode from the exception
1834+
try
1835+
{
1836+
_cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard;
1837+
}
1838+
catch (CultureNotFoundException)
1839+
{
1840+
_cultureCheckState = CultureCheckState.Invariant;
1841+
}
1842+
}
1843+
}
18161844
}
18171845
if (_cultureCheckState == CultureCheckState.Invariant)
18181846
{

0 commit comments

Comments
 (0)