Skip to content

Commit e6428d6

Browse files
authored
Cleanup | Centralise AppContext switches (#3492)
* Move globalization invariant mode detection to LocalAppContextSwitches * Move TruncateScaledDecimal switch to LocalAppContextSwitches * Move UseManagedSNI to LocalAppContextSwitches * Add ILLink.Substitutions.xml resources to trim unused SNI * Remove now-unused constants * Respond to code review on SqlClient Move string constants and variables together; add a comment to UseManagedNetworking explaining that it can be turned into a constant by ILLink. * Sync LocalAppContextSwitchesHelper * Updated TestScaledDecimalParameter tests These tests no longer directly set the AppContext switches to round or truncate decimals, since these switches are cached when first queried. * Remove hardcoded reference to TruncateScaledDecimal switch
1 parent 45839e2 commit e6428d6

File tree

17 files changed

+355
-168
lines changed

17 files changed

+355
-168
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,11 @@
960960

961961
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Windows.cs" />
962962
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectNative.cs" />
963+
964+
<EmbeddedResource Include="$(CommonSourceRoot)Resources\ILLink.Substitutions.Windows.xml">
965+
<LogicalName>ILLink.Substitutions.xml</LogicalName>
966+
<Link>Resources\ILLink.Substitutions.Windows.xml</Link>
967+
</EmbeddedResource>
963968
</ItemGroup>
964969

965970
<!-- Unix only -->
@@ -1002,6 +1007,11 @@
10021007
</Compile>
10031008

10041009
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Unix.cs" />
1010+
1011+
<EmbeddedResource Include="$(CommonSourceRoot)Resources\ILLink.Substitutions.Unix.xml">
1012+
<LogicalName>ILLink.Substitutions.xml</LogicalName>
1013+
<Link>Resources\ILLink.Substitutions.Unix.xml</Link>
1014+
</EmbeddedResource>
10051015
</ItemGroup>
10061016

10071017
<!-- Resources -->

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

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ namespace Microsoft.Data.SqlClient
3131
[DesignerCategory("")]
3232
public sealed partial class SqlConnection : DbConnection, ICloneable
3333
{
34-
private enum CultureCheckState : uint
35-
{
36-
Unknown = 0,
37-
Standard = 1,
38-
Invariant = 2
39-
}
40-
4134
private bool _AsyncCommandInProgress;
4235

4336
// SQLStatistics support
@@ -75,9 +68,6 @@ private enum CultureCheckState : uint
7568
// using SqlConnection.Open() method.
7669
internal bool _applyTransientFaultHandling = false;
7770

78-
// status of invariant culture environment check
79-
private static CultureCheckState _cultureCheckState;
80-
8171
// System column encryption key store providers are added by default
8272
private static readonly Dictionary<string, SqlColumnEncryptionKeyStoreProvider> s_systemColumnEncryptionKeyStoreProviders
8373
= new(capacity: 3, comparer: StringComparer.OrdinalIgnoreCase)
@@ -1956,48 +1946,9 @@ private bool TryOpen(TaskCompletionSource<DbConnectionInternal> retry, SqlConnec
19561946
{
19571947
SqlConnectionString connectionOptions = (SqlConnectionString)ConnectionOptions;
19581948

1959-
if (_cultureCheckState != CultureCheckState.Standard)
1949+
if (LocalAppContextSwitches.GlobalizationInvariantMode)
19601950
{
1961-
// .NET Core 2.0 and up supports a Globalization Invariant Mode to reduce the size of
1962-
// required libraries for applications which don't need globalization support. SqlClient
1963-
// requires those libraries for core functionality and will throw exceptions later if they
1964-
// are not present. Throwing on open with a meaningful message helps identify the issue.
1965-
if (_cultureCheckState == CultureCheckState.Unknown)
1966-
{
1967-
// check if invariant state has been set by appcontext switch directly
1968-
if (AppContext.TryGetSwitch("System.Globalization.Invariant", out bool isEnabled) && isEnabled)
1969-
{
1970-
_cultureCheckState = CultureCheckState.Invariant;
1971-
}
1972-
else
1973-
{
1974-
// check if invariant state has been set through environment variables
1975-
string envValue = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");
1976-
if (string.Equals(envValue, bool.TrueString, StringComparison.OrdinalIgnoreCase) || string.Equals(envValue, "1", StringComparison.OrdinalIgnoreCase))
1977-
{
1978-
_cultureCheckState = CultureCheckState.Invariant;
1979-
}
1980-
else
1981-
{
1982-
// if it hasn't been manually set it could still apply if the os doesn't have
1983-
// icu libs installed or is a native binary with icu support trimmed away
1984-
// netcore 3.1 to net5 do not throw in attempting to create en-us in inariant mode
1985-
// net6 and greater will throw so catch and infer invariant mode from the exception
1986-
try
1987-
{
1988-
_cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard;
1989-
}
1990-
catch (CultureNotFoundException)
1991-
{
1992-
_cultureCheckState = CultureCheckState.Invariant;
1993-
}
1994-
}
1995-
}
1996-
}
1997-
if (_cultureCheckState == CultureCheckState.Invariant)
1998-
{
1999-
throw SQL.GlobalizationInvariantModeNotSupported();
2000-
}
1951+
throw SQL.GlobalizationInvariantModeNotSupported();
20011952
}
20021953

20031954
_applyTransientFaultHandling = (!overrides.HasFlag(SqlConnectionOverrides.OpenWithoutRetry) && connectionOptions != null && connectionOptions.ConnectRetryCount > 0);

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal sealed partial class TdsParser
1111
{
1212
internal void PostReadAsyncForMars()
1313
{
14-
if (TdsParserStateObjectFactory.UseManagedSNI)
14+
if (LocalAppContextSwitches.UseManagedNetworking)
1515
return;
1616

1717
// HACK HACK HACK - for Async only

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ internal sealed partial class TdsParser
6969
// Constants
7070
private const int constBinBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream
7171
private const int constTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader
72-
private const string enableTruncateSwitch = "Switch.Microsoft.Data.SqlClient.TruncateScaledDecimal"; // for applications that need to maintain backwards compatibility with the previous behavior
7372

7473
// State variables
7574
internal TdsParserState _state = TdsParserState.Closed; // status flag for connection
@@ -204,16 +203,6 @@ internal SqlInternalConnectionTds Connection
204203
}
205204
}
206205

207-
private static bool EnableTruncateSwitch
208-
{
209-
get
210-
{
211-
bool value;
212-
value = AppContext.TryGetSwitch(enableTruncateSwitch, out value) ? value : false;
213-
return value;
214-
}
215-
}
216-
217206
internal SqlInternalTransaction CurrentTransaction
218207
{
219208
get
@@ -631,7 +620,7 @@ internal void EnableMars()
631620
// Cache physical stateObj and connection.
632621
_pMarsPhysicalConObj = _physicalStateObj;
633622

634-
if (TdsParserStateObjectFactory.UseManagedSNI)
623+
if (LocalAppContextSwitches.UseManagedNetworking)
635624
_pMarsPhysicalConObj.IncrementPendingCallbacks();
636625

637626
uint info = 0;
@@ -1489,7 +1478,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
14891478
* !=null | == 0 | replace text left of errorMessage
14901479
*/
14911480

1492-
if (TdsParserStateObjectFactory.UseManagedSNI)
1481+
if (LocalAppContextSwitches.UseManagedNetworking)
14931482
{
14941483
Debug.Assert(!string.IsNullOrEmpty(details.ErrorMessage) || details.SniErrorNumber != 0, "Empty error message received from SNI");
14951484
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.TdsParser.ProcessSNIError |ERR|ADV > Empty error message received from SNI. Error Message = {0}, SNI Error Number ={1}", details.ErrorMessage, details.SniErrorNumber);
@@ -1538,7 +1527,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj)
15381527
}
15391528
else
15401529
{
1541-
if (TdsParserStateObjectFactory.UseManagedSNI)
1530+
if (LocalAppContextSwitches.UseManagedNetworking)
15421531
{
15431532
// SNI error. Append additional error message info if available and hasn't been included.
15441533
string sniLookupMessage = SQL.GetSNIErrorMessage(details.SniErrorNumber);
@@ -7653,7 +7642,7 @@ internal static SqlDecimal AdjustSqlDecimalScale(SqlDecimal d, int newScale)
76537642
{
76547643
if (d.Scale != newScale)
76557644
{
7656-
bool round = !EnableTruncateSwitch;
7645+
bool round = !LocalAppContextSwitches.TruncateScaledDecimal;
76577646
return SqlDecimal.AdjustScale(d, newScale - d.Scale, round);
76587647
}
76597648

@@ -7666,7 +7655,7 @@ internal static decimal AdjustDecimalScale(decimal value, int newScale)
76667655

76677656
if (newScale != oldScale)
76687657
{
7669-
bool round = !EnableTruncateSwitch;
7658+
bool round = !LocalAppContextSwitches.TruncateScaledDecimal;
76707659
SqlDecimal num = new SqlDecimal(value);
76717660
num = SqlDecimal.AdjustScale(num, newScale - oldScale, round);
76727661
return num.Value;

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ internal sealed partial class TdsParser
6969
// Constants
7070
private const int constBinBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream
7171
private const int constTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader
72-
private const string enableTruncateSwitch = "Switch.Microsoft.Data.SqlClient.TruncateScaledDecimal"; // for applications that need to maintain backwards compatibility with the previous behavior
7372

7473
// State variables
7574
internal TdsParserState _state = TdsParserState.Closed; // status flag for connection
@@ -205,16 +204,6 @@ internal SqlInternalConnectionTds Connection
205204
}
206205
}
207206

208-
private static bool EnableTruncateSwitch
209-
{
210-
get
211-
{
212-
bool value;
213-
value = AppContext.TryGetSwitch(enableTruncateSwitch, out value) ? value : false;
214-
return value;
215-
}
216-
}
217-
218207
internal SqlInternalTransaction CurrentTransaction
219208
{
220209
get
@@ -7849,7 +7838,7 @@ internal static SqlDecimal AdjustSqlDecimalScale(SqlDecimal d, int newScale)
78497838
{
78507839
if (d.Scale != newScale)
78517840
{
7852-
bool round = !EnableTruncateSwitch;
7841+
bool round = !LocalAppContextSwitches.TruncateScaledDecimal;
78537842
return SqlDecimal.AdjustScale(d, newScale - d.Scale, round);
78547843
}
78557844

@@ -7862,7 +7851,7 @@ internal static decimal AdjustDecimalScale(decimal value, int newScale)
78627851

78637852
if (newScale != oldScale)
78647853
{
7865-
bool round = !EnableTruncateSwitch;
7854+
bool round = !LocalAppContextSwitches.TruncateScaledDecimal;
78667855
SqlDecimal num = new SqlDecimal(value);
78677856
num = SqlDecimal.AdjustScale(num, newScale - oldScale, round);
78687857
return num.Value;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumerator.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private partial DataTable GetDataSourcesInternal()
1515
#if NETFRAMEWORK
1616
return SqlDataSourceEnumeratorNativeHelper.GetDataSources();
1717
#else
18-
return SqlClient.TdsParserStateObjectFactory.UseManagedSNI ? SqlDataSourceEnumeratorManagedHelper.GetDataSources() : SqlDataSourceEnumeratorNativeHelper.GetDataSources();
18+
return SqlClient.LocalAppContextSwitches.UseManagedNetworking ? SqlDataSourceEnumeratorManagedHelper.GetDataSources() : SqlDataSourceEnumeratorNativeHelper.GetDataSources();
1919
#endif
2020
}
2121
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/DbConnectionPoolIdentity.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static WindowsIdentity GetCurrentWindowsIdentity()
2828
#else
2929
internal static DbConnectionPoolIdentity GetCurrent()
3030
{
31-
return TdsParserStateObjectFactory.UseManagedSNI ? GetCurrentManaged() : GetCurrentNative();
31+
return LocalAppContextSwitches.UseManagedNetworking ? GetCurrentManaged() : GetCurrentNative();
3232
}
3333
#endif
3434

0 commit comments

Comments
 (0)