HangfireIO/Hangfire · error · ArgumentException

The value must be a positive number

Error message

The value must be a positive number

What it means

ArgumentException thrown by SqlServerConnection.GetFirstByLowestScoreFromSet(string,double,double,int) when the requested count is less than or equal to zero. The method maps directly to a 'select top (@count)' query, which requires a strictly positive integer.

Source

Thrown at src/Hangfire.SqlServer/SqlServerConnection.cs:407

                var result = connection.Query<string>(
                    query,
                    new { key = key },
                    commandTimeout: storage.CommandTimeout);

                return new HashSet<string>(result);
            }, key);
        }

        public override string GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore)
        {
            return GetFirstByLowestScoreFromSet(key, fromScore, toScore, 1).FirstOrDefault();
        }

        public override List<string> GetFirstByLowestScoreFromSet(string key, double fromScore, double toScore, int count)
        {
            if (key == null) throw new ArgumentNullException(nameof(key));
            if (count <= 0) throw new ArgumentException("The value must be a positive number", nameof(count));
            if (toScore < fromScore) throw new ArgumentException("The `toScore` value must be higher or equal to the `fromScore` value.", nameof(toScore));

            return _storage.UseConnection(_dedicatedConnection, static (storage, connection, pair) =>
            {
                var query = storage.GetQueryFromTemplate(static schemaName =>
$@"select top (@count) Value from [{schemaName}].[Set] with (forceseek) where [Key] = @key and Score between @from and @to order by Score");

                var result = connection.Query<string>(
                    query,
                    new { count = pair.Value.Item3, key = pair.Key, from = pair.Value.Item1, to = pair.Value.Item2 },
                    commandTimeout: storage.CommandTimeout);

                return result.ToList();
            }, new KeyValuePair<string, ValueTriple<double, double, int>>(key, CreateTriple(fromScore, toScore, count)));
        }

        public override void SetRangeInHash(string key, IEnumerable<KeyValuePair<string, string>> keyValuePairs)
        {

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Validate that count is >= 1 before calling, and default to a sensible positive value (e.g. 1) when unset.
  2. Guard pagination inputs at the boundary: if (count < 1) count = 1;.

Example fix

// before
var items = connection.GetFirstByLowestScoreFromSet(key, from, to, requestedCount); // requestedCount could be 0

// after
var items = connection.GetFirstByLowestScoreFromSet(key, from, to, Math.Max(1, requestedCount));
Defensive patterns

Strategy: validation

Validate before calling

static List<string> SafeGetFirstByLowestScore(IStorageConnection conn, string key, double from, double to, int count)
{
    if (count < 1) count = 1;
    return conn.GetFirstByLowestScoreFromSet(key, from, to, count);
}

Prevention

When it happens

Trigger: Calling GetFirstByLowestScoreFromSet with a count argument of 0 or a negative number (often via a derived/monitoring caller that computes count from a configuration value or pagination).

Common situations: A 'take' / 'pageSize' variable defaulting to 0; computing count as a difference that evaluated to 0 or negative; misconfigured limit passed from the dashboard or an API.

Related errors


AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13). Data as JSON: /api/errors/a3a188b8f15c9f3f. Report an issue: GitHub.