Skip to content

Commit

Permalink
Don't try to read nullable types from ADO.NET directly
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Dec 16, 2024
1 parent fbda21a commit d0d6cc8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Hangfire.SqlServer/DbCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ public static DbCommand AddExpandedParameter<T>(
return command;
}

public static T GetParameterValue<T>([NotNull] this DbParameter parameter)
{
if (parameter == null) throw new ArgumentNullException(nameof(parameter));

switch (parameter.Value)
{
case null or DBNull: return default;
case T typed: return typed;
default:
var type = typeof(T);
type = Nullable.GetUnderlyingType(type) ?? type;
return (T)Convert.ChangeType(parameter.Value, type, CultureInfo.InvariantCulture);
}
}

private static DbParameter AddParameterInternal(
DbCommand command,
string parameterName,
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.SqlServer/SqlServerWriteOnlyTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override void Commit()

foreach (var lockCommand in ctx._lockCommands)
{
var releaseResult = (int?) lockCommand.Item2.Value;
var releaseResult = lockCommand.Item2.GetParameterValue<int?>();
if (releaseResult.HasValue && releaseResult.Value < 0)
{
throw new SqlServerDistributedLockException($"Could not release a lock on the resource '{lockCommand.Item3}': Server returned the '{releaseResult}' error.");
Expand Down

0 comments on commit d0d6cc8

Please sign in to comment.