diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs index 8355d72d6f..750c629716 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs @@ -60,14 +60,14 @@ private static DataTable ParseServerEnumString(string serverInstances) if (InstanceDetails.Count > 0) { dataRow = dataTable.NewRow(); - dataRow[0] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.ServerNameCol) == true ? - InstanceDetails[SqlDataSourceEnumeratorUtil.ServerNameCol] : string.Empty; - dataRow[1] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.InstanceNameCol) == true ? - InstanceDetails[SqlDataSourceEnumeratorUtil.InstanceNameCol] : string.Empty; - dataRow[2] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.IsClusteredCol) == true ? - InstanceDetails[SqlDataSourceEnumeratorUtil.IsClusteredCol] : string.Empty; - dataRow[3] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.VersionNameCol) == true ? - InstanceDetails[SqlDataSourceEnumeratorUtil.VersionNameCol] : string.Empty; + dataRow[0] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.ServerNameCol, out string serverName) ? + serverName : string.Empty; + dataRow[1] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.InstanceNameCol, out string instanceName) ? + instanceName : string.Empty; + dataRow[2] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.IsClusteredCol, out string isClustered) ? + isClustered : string.Empty; + dataRow[3] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.VersionNameCol, out string versionName) ? + versionName : string.Empty; dataTable.Rows.Add(dataRow); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs index 4dd3086b45..07ddd6fd50 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs @@ -28,13 +28,8 @@ internal bool AddDNSInfo(SQLDNSInfo item) { if (item != null) { - if (DNSInfoCache.ContainsKey(item.FQDN)) - { - - DeleteDNSInfo(item.FQDN); - } - - return DNSInfoCache.TryAdd(item.FQDN, item); + DNSInfoCache[item.FQDN] = item; + return true; } return false; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs index d9cd9c798e..cdbaa27683 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs @@ -816,29 +816,21 @@ private static bool AddToServerUserHash(string server, IdentityUserNamePair iden { Dictionary> identityDatabaseHash; - if (!s_serverUserHash.ContainsKey(server)) + if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash)) { SqlClientEventSource.Log.TryNotificationTraceEvent(" Hash did not contain server, adding."); identityDatabaseHash = new Dictionary>(); s_serverUserHash.Add(server, identityDatabaseHash); } - else - { - identityDatabaseHash = s_serverUserHash[server]; - } List databaseServiceList; - if (!identityDatabaseHash.ContainsKey(identityUser)) + if (!identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList)) { SqlClientEventSource.Log.TryNotificationTraceEvent(" Hash contained server but not user, adding user."); databaseServiceList = new List(); identityDatabaseHash.Add(identityUser, databaseServiceList); } - else - { - databaseServiceList = identityDatabaseHash[identityUser]; - } if (!databaseServiceList.Contains(databaseService)) { @@ -869,15 +861,12 @@ private static void RemoveFromServerUserHash(string server, IdentityUserNamePair { Dictionary> identityDatabaseHash; - if (s_serverUserHash.ContainsKey(server)) + if (s_serverUserHash.TryGetValue(server, out identityDatabaseHash)) { - identityDatabaseHash = s_serverUserHash[server]; - List databaseServiceList; - if (identityDatabaseHash.ContainsKey(identityUser)) + if (identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList)) { - databaseServiceList = identityDatabaseHash[identityUser]; int index = databaseServiceList.IndexOf(databaseService); if (index >= 0) @@ -933,7 +922,9 @@ internal static string GetDefaultComposedOptions(string server, string failoverS lock (s_serverUserHash) { - if (!s_serverUserHash.ContainsKey(server)) + Dictionary> identityDatabaseHash; + + if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash)) { if (0 == s_serverUserHash.Count) { @@ -941,7 +932,7 @@ internal static string GetDefaultComposedOptions(string server, string failoverS SqlClientEventSource.Log.TryNotificationTraceEvent(" ERROR - no start calls have been made, about to throw."); throw SQL.SqlDepDefaultOptionsButNoStart(); } - else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.ContainsKey(failoverServer)) + else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.TryGetValue(failoverServer, out identityDatabaseHash)) { SqlClientEventSource.Log.TryNotificationTraceEvent(" using failover server instead\n"); server = failoverServer; @@ -953,11 +944,9 @@ internal static string GetDefaultComposedOptions(string server, string failoverS } } - Dictionary> identityDatabaseHash = s_serverUserHash[server]; - List databaseList = null; - if (!identityDatabaseHash.ContainsKey(identityUser)) + if (!identityDatabaseHash.TryGetValue(identityUser, out databaseList)) { if (identityDatabaseHash.Count > 1) { @@ -976,10 +965,6 @@ internal static string GetDefaultComposedOptions(string server, string failoverS } } } - else - { - databaseList = identityDatabaseHash[identityUser]; - } DatabaseServicePair pair = new(database, null); DatabaseServicePair resultingPair = null; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs index 208b3d6693..84f0965090 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs @@ -251,11 +251,10 @@ internal bool AppDomainUnload(string appDomainKey) // For each decrement, subtract from count, and delete if we reach 0. lock (_appDomainKeyHash) { - if (_appDomainKeyHash.ContainsKey(appDomainKey)) + if (_appDomainKeyHash.TryGetValue(appDomainKey, out int value)) { // Do nothing if AppDomain did not call Start! SqlClientEventSource.Log.TryNotificationTraceEvent(" _appDomainKeyHash contained AppDomainKey: '{0}'.", appDomainKey); - int value = _appDomainKeyHash[appDomainKey]; SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP> _appDomainKeyHash for AppDomainKey: '{0}' count: '{1}'.", appDomainKey, value); Debug.Assert(value > 0, "Why is value 0 or less?"); @@ -271,9 +270,9 @@ internal bool AppDomainUnload(string appDomainKey) Debug.Assert(0 == value, "We did not reach 0 at end of loop in AppDomainUnload!"); Debug.Assert(!_appDomainKeyHash.ContainsKey(appDomainKey), "Key not removed after AppDomainUnload!"); - if (_appDomainKeyHash.ContainsKey(appDomainKey)) + if (_appDomainKeyHash.TryGetValue(appDomainKey, out int remainingCount)) { - SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, _appDomainKeyHash[appDomainKey]); + SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, remainingCount); } } else @@ -509,10 +508,11 @@ internal void IncrementStartCount(string appDomainKey, out bool appDomainStart) // For each increment, add to count, and create entry if not present. lock (_appDomainKeyHash) { - if (_appDomainKeyHash.ContainsKey(appDomainKey)) + if (_appDomainKeyHash.TryGetValue(appDomainKey, out int count)) { - _appDomainKeyHash[appDomainKey] = _appDomainKeyHash[appDomainKey] + 1; - SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, _appDomainKeyHash[appDomainKey]); + count++; + _appDomainKeyHash[appDomainKey] = count; + SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, count); } else { @@ -1617,7 +1617,7 @@ private bool Start( { if (!_sqlDependencyPerAppDomainDispatchers.ContainsKey(appDomainKey)) { - _sqlDependencyPerAppDomainDispatchers[appDomainKey] = dispatcher; + _sqlDependencyPerAppDomainDispatchers.Add(appDomainKey, dispatcher); } } @@ -1636,7 +1636,7 @@ private bool Start( SqlConnectionContainer container = null; lock (_connectionContainers) { - if (!_connectionContainers.ContainsKey(hashHelper)) + if (!_connectionContainers.TryGetValue(hashHelper, out container)) { SqlClientEventSource.Log.TryNotificationTraceEvent(" {0}, hashtable miss, creating new container.", ObjectID); container = new SqlConnectionContainer(hashHelper, appDomainKey, useDefaults); @@ -1646,7 +1646,6 @@ private bool Start( } else { - container = _connectionContainers[hashHelper]; SqlClientEventSource.Log.TryNotificationTraceEvent(" {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID); if (container.InErrorState) { @@ -1712,9 +1711,8 @@ internal bool Stop( lock (_connectionContainers) { - if (_connectionContainers.ContainsKey(hashHelper)) + if (_connectionContainers.TryGetValue(hashHelper, out SqlConnectionContainer container)) { - SqlConnectionContainer container = _connectionContainers[hashHelper]; SqlClientEventSource.Log.TryNotificationTraceEvent(" {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID); server = container.Server; // Return server, database, and queue info for use by calling SqlDependency. database = container.Database; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs index 9eebc0e141..693ee1dd1e 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs @@ -364,11 +364,7 @@ internal SqlDependency LookupDependencyEntry(string id) lock (_instanceLock) { - if (_dependencyIdToDependencyHash.ContainsKey(id)) - { - entry = _dependencyIdToDependencyHash[id]; - } - else + if (!_dependencyIdToDependencyHash.TryGetValue(id, out entry)) { SqlClientEventSource.Log.TryNotificationTraceEvent(" ERROR - dependency ID mismatch - not throwing."); }