From 08bfb61acc0ce00704858267f2fb6cd750c985c2 Mon Sep 17 00:00:00 2001 From: Erwin Bovendeur Date: Tue, 3 Dec 2024 22:03:29 +0100 Subject: [PATCH] #97: Update Hangfire.SQLServer to reflect changes in Hangfire 1.8.15 --- appveyor.yml | 1 + .../Hangfire.Core.MvcApplication.csproj | 2 +- .../Hangfire.MvcApplication.csproj | 8 +-- .../Hangfire.MvcApplication/packages.config | 4 +- .../HangfireServerOne.csproj | 4 +- .../HangfireServerTwo.csproj | 4 +- .../MultipleDashboards.csproj | 4 +- .../Properties/launchSettings.json | 23 +++----- .../DbCommandExtensions.cs | 52 +++++++++++++++++++ .../FaceIT.Hangfire.Tags.SqlServer.csproj | 4 +- .../SqlTagsTransaction.cs | 26 +++------- 11 files changed, 83 insertions(+), 49 deletions(-) create mode 100644 src/FaceIT.Hangfire.Tags.SqlServer/DbCommandExtensions.cs diff --git a/appveyor.yml b/appveyor.yml index 9b1ceae..8a77b5a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,6 +9,7 @@ environment: branches: only: - main + - feature/issue-97 pull_requests: do_not_increment_build_number: true diff --git a/samples/Hangfire.Core.MvcApplication/Hangfire.Core.MvcApplication.csproj b/samples/Hangfire.Core.MvcApplication/Hangfire.Core.MvcApplication.csproj index ef32571..277e59f 100644 --- a/samples/Hangfire.Core.MvcApplication/Hangfire.Core.MvcApplication.csproj +++ b/samples/Hangfire.Core.MvcApplication/Hangfire.Core.MvcApplication.csproj @@ -6,7 +6,7 @@ - + diff --git a/samples/Hangfire.MvcApplication/Hangfire.MvcApplication.csproj b/samples/Hangfire.MvcApplication/Hangfire.MvcApplication.csproj index 9ad8a8b..a9ad697 100644 --- a/samples/Hangfire.MvcApplication/Hangfire.MvcApplication.csproj +++ b/samples/Hangfire.MvcApplication/Hangfire.MvcApplication.csproj @@ -59,8 +59,8 @@ ..\..\packages\DnsClient.1.6.1\lib\net471\DnsClient.dll - - ..\..\packages\Hangfire.Core.1.8.2\lib\net46\Hangfire.Core.dll + + ..\..\packages\Hangfire.Core.1.8.15\lib\net46\Hangfire.Core.dll ..\..\packages\Hangfire.Mongo.1.9.6\lib\netstandard2.0\Hangfire.Mongo.dll @@ -77,8 +77,8 @@ ..\..\packages\Hangfire.Redis.StackExchange.1.8.7\lib\net462\Hangfire.Redis.StackExchange.dll - - ..\..\packages\Hangfire.SqlServer.1.8.2\lib\net451\Hangfire.SqlServer.dll + + ..\..\packages\Hangfire.SqlServer.1.8.15\lib\net451\Hangfire.SqlServer.dll ..\..\packages\Hangfire.Storage.SQLite.0.3.4\lib\net48\Hangfire.Storage.SQLite.dll diff --git a/samples/Hangfire.MvcApplication/packages.config b/samples/Hangfire.MvcApplication/packages.config index a723347..762de42 100644 --- a/samples/Hangfire.MvcApplication/packages.config +++ b/samples/Hangfire.MvcApplication/packages.config @@ -5,12 +5,12 @@ - + - + diff --git a/samples/MultipleDashboards/HangfireServerOne/HangfireServerOne.csproj b/samples/MultipleDashboards/HangfireServerOne/HangfireServerOne.csproj index 6cf2520..c47d5d2 100644 --- a/samples/MultipleDashboards/HangfireServerOne/HangfireServerOne.csproj +++ b/samples/MultipleDashboards/HangfireServerOne/HangfireServerOne.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/samples/MultipleDashboards/HangfireServerTwo/HangfireServerTwo.csproj b/samples/MultipleDashboards/HangfireServerTwo/HangfireServerTwo.csproj index 788c4b5..5171ada 100644 --- a/samples/MultipleDashboards/HangfireServerTwo/HangfireServerTwo.csproj +++ b/samples/MultipleDashboards/HangfireServerTwo/HangfireServerTwo.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/samples/MultipleDashboards/MultipleDashboards/MultipleDashboards.csproj b/samples/MultipleDashboards/MultipleDashboards/MultipleDashboards.csproj index 8691da1..a954d9e 100644 --- a/samples/MultipleDashboards/MultipleDashboards/MultipleDashboards.csproj +++ b/samples/MultipleDashboards/MultipleDashboards/MultipleDashboards.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/samples/MultipleDashboards/MultipleDashboards/Properties/launchSettings.json b/samples/MultipleDashboards/MultipleDashboards/Properties/launchSettings.json index c4b6d8d..3e95dfd 100644 --- a/samples/MultipleDashboards/MultipleDashboards/Properties/launchSettings.json +++ b/samples/MultipleDashboards/MultipleDashboards/Properties/launchSettings.json @@ -1,20 +1,5 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:54863", - "sslPort": 44325 - } - }, "profiles": { - "IIS Express": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, "MultipleDashboards": { "commandName": "Project", "launchBrowser": true, @@ -24,5 +9,13 @@ }, "applicationUrl": "https://localhost:5001;http://localhost:5000" } + }, + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:54863", + "sslPort": 44325 + } } } \ No newline at end of file diff --git a/src/FaceIT.Hangfire.Tags.SqlServer/DbCommandExtensions.cs b/src/FaceIT.Hangfire.Tags.SqlServer/DbCommandExtensions.cs new file mode 100644 index 0000000..24e729e --- /dev/null +++ b/src/FaceIT.Hangfire.Tags.SqlServer/DbCommandExtensions.cs @@ -0,0 +1,52 @@ +using System; +using System.Data.Common; +using System.Data; +using Hangfire.Annotations; + +namespace Hangfire.Tags.SqlServer +{ + internal static class DbCommandExtensions + { + public static DbCommand Create( + [NotNull] this DbConnection connection, + [NotNull] string text, + CommandType type = CommandType.Text, + int? timeout = null) + { + if (connection == null) throw new ArgumentNullException(nameof(connection)); + if (text == null) throw new ArgumentNullException(nameof(text)); + + var command = connection.CreateCommand(); + command.CommandType = type; + command.CommandText = text; + + if (timeout.HasValue) + { + command.CommandTimeout = timeout.Value; + } + + return command; + } + + public static DbCommand AddParameter( + [NotNull] this DbCommand command, + [NotNull] string parameterName, + [CanBeNull] object value, + DbType dbType, + [CanBeNull] int? size = null) + { + if (command == null) throw new ArgumentNullException(nameof(command)); + if (parameterName == null) throw new ArgumentNullException(nameof(parameterName)); + + var parameter = command.CreateParameter(); + parameter.ParameterName = parameterName; + parameter.DbType = dbType; + parameter.Value = value ?? DBNull.Value; + + if (size.HasValue) parameter.Size = size.Value; + + command.Parameters.Add(parameter); + return command; + } + } +} diff --git a/src/FaceIT.Hangfire.Tags.SqlServer/FaceIT.Hangfire.Tags.SqlServer.csproj b/src/FaceIT.Hangfire.Tags.SqlServer/FaceIT.Hangfire.Tags.SqlServer.csproj index 9a171f4..aaae9ef 100644 --- a/src/FaceIT.Hangfire.Tags.SqlServer/FaceIT.Hangfire.Tags.SqlServer.csproj +++ b/src/FaceIT.Hangfire.Tags.SqlServer/FaceIT.Hangfire.Tags.SqlServer.csproj @@ -5,7 +5,7 @@ false Erwin Bovendeur faceit - 1.9.2 + 1.9.15-beta.1 Support for SQL Server for Hangfire.Tags. This seperate library is required in order to search for tags, and proper cleanup. https://github.com/face-it/Hangfire.Tags @@ -32,7 +32,7 @@ - + diff --git a/src/FaceIT.Hangfire.Tags.SqlServer/SqlTagsTransaction.cs b/src/FaceIT.Hangfire.Tags.SqlServer/SqlTagsTransaction.cs index 81fb5ba..0f21992 100644 --- a/src/FaceIT.Hangfire.Tags.SqlServer/SqlTagsTransaction.cs +++ b/src/FaceIT.Hangfire.Tags.SqlServer/SqlTagsTransaction.cs @@ -1,7 +1,8 @@ using System; using System.Data; -using System.Linq; +using System.Data.Common; using System.Reflection; +using Dapper; using Hangfire.SqlServer; using Hangfire.Storage; using Hangfire.Tags.Storage; @@ -26,9 +27,6 @@ internal class SqlTagsTransaction : ITagsTransaction private static Type _type; private static MethodInfo _acquireSetLock; - private static Type _sqlCommandBatchParameter; - private static ConstructorInfo _sqlCommandBatchParameterCtor; - private static PropertyInfo _sqlCommandBatchParameterValue; private static MethodInfo _addCommand; @@ -67,14 +65,6 @@ public SqlTagsTransaction(SqlServerStorageOptions options, IWriteOnlyTransaction if (_addCommand == null) throw new ArgumentException("The functions QueueCommand and AddCommand cannot be found."); - - if (_sqlCommandBatchParameter == null) - { - _sqlCommandBatchParameter = Type.GetType("Hangfire.SqlServer.SqlCommandBatchParameter, Hangfire.SqlServer"); - _sqlCommandBatchParameterCtor = - _sqlCommandBatchParameter.GetConstructor(new Type[] {typeof(string), typeof(DbType), typeof(int?)}); - _sqlCommandBatchParameterValue = _sqlCommandBatchParameter.GetProperty("Value"); - } } private void AcquireSetLock(string key) @@ -85,17 +75,15 @@ private void AcquireSetLock(string key) private void AddCommand(string commandText, string key, params SqlCommandBatchParameter[] parameters) { - var batchParams = Array.CreateInstance(_sqlCommandBatchParameter, parameters.Length); - for (var i = 0; i < parameters.Length; i++) + DbCommand Command(DbConnection connection) { - var p = parameters[i]; - var param = _sqlCommandBatchParameterCtor.Invoke(new object[] { p.ParameterName, p.DbType, null }); - _sqlCommandBatchParameterValue.SetValue(param, p.Value); - batchParams.SetValue(param, i); + var cmd = connection.Create(commandText); + foreach (var sqlParameter in parameters) cmd.AddParameter(sqlParameter.ParameterName, sqlParameter.Value, sqlParameter.DbType); + return cmd; } var setCommands = _transaction.GetType().GetTypeInfo().GetField("_setCommands", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_transaction); - _addCommand.Invoke(_transaction, new object[] { setCommands, key, commandText, batchParams }); + _addCommand.Invoke(_transaction, new[] {setCommands, key, (Func) Command}); } public void ExpireSetValue(string key, string value, TimeSpan expireIn)