From 9d81d15297d143b0f6ce2b83e2ddfb275a868c08 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Thu, 14 Oct 2021 14:08:01 -0700 Subject: [PATCH 1/3] Merge netfx to netcore for SqlDependencyUtils.cs --- .../src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs index 15f2b573e2..997f74b0e9 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs @@ -86,7 +86,12 @@ private SqlDependencyPerAppDomainDispatcher() _dependencyIdToDependencyHash = new Dictionary(); _notificationIdToDependenciesHash = new Dictionary(); _commandHashToNotificationId = new Dictionary(); +#if NETFRAMEWORK + _timeoutTimer = new Timer(new TimerCallback(TimeoutTimerCallback), null, Timeout.Infinite, Timeout.Infinite); + // If rude abort - we'll leak. This is acceptable for now. + AppDomain.CurrentDomain.DomainUnload += new EventHandler(this.UnloadEventHandler); +#else _timeoutTimer = ADP.UnsafeCreateTimer( new TimerCallback(TimeoutTimerCallback), null, @@ -95,6 +100,7 @@ private SqlDependencyPerAppDomainDispatcher() SubscribeToAppDomainUnload(); SubscribeToAssemblyLoadContextUnload(); +#endif // NETFRAMEWORK } finally { From 687cb531f31d9848cdd6cd3bb7eae4481968475e Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Thu, 14 Oct 2021 14:19:31 -0700 Subject: [PATCH 2/3] Move SqlDependencyUtils.cs, SqlDependencyUtils.AppDomain.cs, SqlDependencyUtils.AssemblyLoadContext.cs to shared src and update reference in the csprojs --- .../netcore/src/Microsoft.Data.SqlClient.csproj | 12 +++++++++--- .../netfx/src/Microsoft.Data.SqlClient.csproj | 4 +++- .../Data/SqlClient/SqlDependencyUtils.AppDomain.cs | 0 .../SqlDependencyUtils.AssemblyLoadContext.cs | 0 .../Microsoft/Data/SqlClient/SqlDependencyUtils.cs | 0 5 files changed, 12 insertions(+), 4 deletions(-) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AppDomain.cs (100%) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AssemblyLoadContext.cs (100%) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs (100%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 6cee49f62c..460ca20b83 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -259,6 +259,12 @@ Microsoft\Data\SqlClient\SqlDataAdapter.cs + + Microsoft\Data\SqlClient\SqlDependencyUtils.cs + + + Microsoft\Data\SqlClient\SqlDependencyUtils.AppDomain.cs + Microsoft\Data\SqlClient\SqlErrorCollection.cs @@ -455,6 +461,9 @@ + + Microsoft\Data\SqlClient\SqlDependencyUtils.AssemblyLoadContext.cs + @@ -463,7 +472,6 @@ - @@ -538,8 +546,6 @@ - - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index dccab48852..2c90220e09 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -336,6 +336,9 @@ Microsoft\Data\SqlClient\SqlDataAdapter.cs + + Microsoft\Data\SqlClient\SqlDependencyUtils.cs + Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.Crypto.cs @@ -527,7 +530,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AppDomain.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AppDomain.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AppDomain.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AppDomain.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AssemblyLoadContext.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AssemblyLoadContext.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AssemblyLoadContext.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.AssemblyLoadContext.cs diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs From 9708cbac0b464fabfb1e54ff33ae9de78decf0df Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Thu, 14 Oct 2021 14:32:10 -0700 Subject: [PATCH 3/3] Update file to conform with coding style cleaning up IDE1006, IDE0090, IDE0044, IDE0008, and IDE0003 --- .../Data/SqlClient/SqlDependencyUtils.cs | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) 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 997f74b0e9..404e27d788 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs @@ -18,15 +18,14 @@ internal partial class SqlDependencyPerAppDomainDispatcher : MarshalByRefObject { // Instance members - internal static readonly SqlDependencyPerAppDomainDispatcher - SingletonInstance = new SqlDependencyPerAppDomainDispatcher(); // singleton object + internal static readonly SqlDependencyPerAppDomainDispatcher SingletonInstance = new(); // singleton object - internal object _instanceLock = new object(); + internal object _instanceLock = new(); // Dependency ID -> Dependency hashtable. 1 -> 1 mapping. // 1) Used for ASP.NET to map from ID to dependency. // 2) Used to enumerate dependencies to invalidate based on server. - private Dictionary _dependencyIdToDependencyHash; + private readonly Dictionary _dependencyIdToDependencyHash; // holds dependencies list per notification and the command hash from which this notification was generated // command hash is needed to remove its entry from _commandHashToNotificationId when the notification is removed @@ -46,12 +45,12 @@ internal DependencyList(string commandHash) // resource effect on SQL Server. The Guid identifier is sent to the server during notification enlistment, // and returned during the notification event. Dependencies look up existing Guids, if one exists, to ensure // they are re-using notification ids. - private Dictionary _notificationIdToDependenciesHash; + private readonly Dictionary _notificationIdToDependenciesHash; // CommandHash value -> notificationId associated with it: 1->1 mapping. This map is used to quickly find if we need to create // new notification or hookup into existing one. // CommandHash is built from connection string, command text and parameters - private Dictionary _commandHashToNotificationId; + private readonly Dictionary _commandHashToNotificationId; // TIMEOUT LOGIC DESCRIPTION // @@ -73,10 +72,10 @@ internal DependencyList(string commandHash) private DateTime _nextTimeout; // Timer to periodically check the dependencies in the table and see if anyone needs // a timeout. We'll enable this only on demand. - private Timer _timeoutTimer; + private readonly Timer _timeoutTimer; - private static int _objectTypeCount; // EventSource counter - internal int ObjectID { get; } = Interlocked.Increment(ref _objectTypeCount); + private static int s_objectTypeCount; // EventSource counter + internal int ObjectID { get; } = Interlocked.Increment(ref s_objectTypeCount); private SqlDependencyPerAppDomainDispatcher() { @@ -90,7 +89,7 @@ private SqlDependencyPerAppDomainDispatcher() _timeoutTimer = new Timer(new TimerCallback(TimeoutTimerCallback), null, Timeout.Infinite, Timeout.Infinite); // If rude abort - we'll leak. This is acceptable for now. - AppDomain.CurrentDomain.DomainUnload += new EventHandler(this.UnloadEventHandler); + AppDomain.CurrentDomain.DomainUnload += new EventHandler(UnloadEventHandler); #else _timeoutTimer = ADP.UnsafeCreateTimer( new TimerCallback(TimeoutTimerCallback), @@ -108,10 +107,11 @@ private SqlDependencyPerAppDomainDispatcher() } } +#if NETCOREAPP || NETSTANDARD partial void SubscribeToAppDomainUnload(); partial void SubscribeToAssemblyLoadContextUnload(); - +#endif private void UnloadEventHandler(object sender, EventArgs e) { long scopeID = SqlClientEventSource.Log.TryNotificationScopeEnterEvent("SqlDependencyPerAppDomainDispatcher.UnloadEventHandler | DEP | Object Id {0}", ObjectID); @@ -121,7 +121,7 @@ private void UnloadEventHandler(object sender, EventArgs e) // stopping of all start calls in this AppDomain. For containers shared among various AppDomains, // this will just be a ref-count subtract. For non-shared containers, we will close the container // and clean-up. - var dispatcher = SqlDependency.ProcessDispatcher; + SqlDependencyProcessDispatcher dispatcher = SqlDependency.ProcessDispatcher; dispatcher?.QueueAppDomainUnloading(SqlDependency.AppDomainKey); } finally @@ -177,8 +177,7 @@ internal string AddCommandEntry(string commandHash, SqlDependency dep) { // we have one or more SqlDependency instances with same command hash - DependencyList dependencyList = null; - if (!_notificationIdToDependenciesHash.TryGetValue(notificationId, out dependencyList)) + if (!_notificationIdToDependenciesHash.TryGetValue(notificationId, out DependencyList dependencyList)) { // this should not happen since _commandHashToNotificationId and _notificationIdToDependenciesHash are always // updated together @@ -209,7 +208,7 @@ internal string AddCommandEntry(string commandHash, SqlDependency dep) Guid.NewGuid().ToString("D", System.Globalization.CultureInfo.InvariantCulture) ); SqlClientEventSource.Log.TryNotificationTraceEvent(" Creating new Dependencies list for commandHash."); - DependencyList dependencyList = new DependencyList(commandHash); + DependencyList dependencyList = new(commandHash); dependencyList.Add(dep); // map command hash to notification we just created to reuse it for the next client @@ -295,7 +294,7 @@ internal void InvalidateServer(string server, SqlNotification sqlNotification) long scopeID = SqlClientEventSource.Log.TryNotificationScopeEnterEvent(" {0}, server: '{1}'", ObjectID, server); try { - List dependencies = new List(); + List dependencies = new(); lock (_instanceLock) { // Copy inside of lock, but invalidate outside of lock. @@ -455,8 +454,8 @@ private void RemoveDependencyFromCommandToDependenciesHash(SqlDependency depende { lock (_instanceLock) { - List notificationIdsToRemove = new List(); - List commandHashesToRemove = new List(); + List notificationIdsToRemove = new(); + List commandHashesToRemove = new(); foreach (KeyValuePair entry in _notificationIdToDependenciesHash) {