@@ -27,6 +27,12 @@ namespace Microsoft.Data.SqlClient
2727 /// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlConnection.xml' path='docs/members[@name="SqlConnection"]/SqlConnection/*' />
2828 public sealed partial class SqlConnection : DbConnection , ICloneable
2929 {
30+ private enum CultureCheckState : uint
31+ {
32+ Unknown = 0 ,
33+ Standard = 1 ,
34+ Invariant = 2
35+ }
3036
3137 private bool _AsyncCommandInProgress ;
3238
@@ -72,7 +78,9 @@ private static readonly Dictionary<string, SqlColumnEncryptionKeyStoreProvider>
7278 } ;
7379
7480 // Lock to control setting of _CustomColumnEncryptionKeyStoreProviders
75- private static readonly Object _CustomColumnEncryptionKeyProvidersLock = new Object ( ) ;
81+ private static readonly object _CustomColumnEncryptionKeyProvidersLock = new object ( ) ;
82+ // status of invariant culture environment check
83+ private static CultureCheckState _cultureCheckState ;
7684
7785 /// <summary>
7886 /// Custom provider list should be provided by the user. We shallow copy the user supplied dictionary into a ReadOnlyDictionary.
@@ -1421,13 +1429,20 @@ private bool TryOpen(TaskCompletionSource<DbConnectionInternal> retry)
14211429 {
14221430 SqlConnectionString connectionOptions = ( SqlConnectionString ) ConnectionOptions ;
14231431
1424- // .NET Core 2.0 and up supports a Globalization Invariant Mode to reduce the size of
1425- // required libraries for applications which don't need globalization support. SqlClient
1426- // requires those libraries for core functionality and will throw exceptions later if they
1427- // are not present. Throwing on open with a meaningful message helps identify the issue.
1428- if ( CultureInfo . GetCultureInfo ( "en-US" ) . EnglishName . Contains ( "Invariant" ) )
1432+ if ( _cultureCheckState != CultureCheckState . Standard )
14291433 {
1430- throw SQL . GlobalizationInvariantModeNotSupported ( ) ;
1434+ // .NET Core 2.0 and up supports a Globalization Invariant Mode to reduce the size of
1435+ // required libraries for applications which don't need globalization support. SqlClient
1436+ // requires those libraries for core functionality and will throw exceptions later if they
1437+ // are not present. Throwing on open with a meaningful message helps identify the issue.
1438+ if ( _cultureCheckState == CultureCheckState . Unknown )
1439+ {
1440+ _cultureCheckState = CultureInfo . GetCultureInfo ( "en-US" ) . EnglishName . Contains ( "Invariant" ) ? CultureCheckState . Invariant : CultureCheckState . Standard ;
1441+ }
1442+ if ( _cultureCheckState == CultureCheckState . Invariant )
1443+ {
1444+ throw SQL . GlobalizationInvariantModeNotSupported ( ) ;
1445+ }
14311446 }
14321447
14331448 _applyTransientFaultHandling = ( retry == null && connectionOptions != null && connectionOptions . ConnectRetryCount > 0 ) ;
0 commit comments