HangfireIO/Hangfire · warning · InvalidCastException

Invalid Cron Expression

Error message

Invalid Cron Expression

What it means

Cron.GetDescription (Cron.cs:293) is an obsolete helper that validates the cron string by splitting on spaces and requiring exactly 5 fields (standard cron format). Fewer or more fields cause an InvalidCastException. The method is deprecated in favor of the CronExpressionDescriptor package.

Source

Thrown at src/Hangfire.Core/Cron.cs:293

        public static string MonthInterval(int interval)
        {
            return $"0 0 1 */{interval} *";
        }

#if FEATURE_CRONDESCRIPTOR
        /// <summary>
        /// Converts a Cron expression string into a description.
        /// </summary>
        /// <param name="cronExpression">A Cron expression string.</param>
        /// <returns>English description.</returns>
        [Obsolete("Please install `CronExpressionDescriptor` package manually and use it.")]
        public static string GetDescription(string cronExpression)
        {
            string[] expressionParts = cronExpression.Split(' ');

            if (expressionParts.Length != 5)
            {
                throw new InvalidCastException("Invalid Cron Expression");
            }

            foreach (string expressionPart in expressionParts)
            {
                int num;
                if (!Int32.TryParse(expressionPart, out num) && expressionPart != "*")
                {
                    throw new InvalidCastException("Invalid Cron Expression");
                }
            }

            return CronExpressionDescriptor.ExpressionDescriptor.GetDescription(cronExpression);
        }
#endif
    }
}

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Provide a standard 5-field cron expression (minute hour day month day-of-week).
  2. Install and use the CronExpressionDescriptor package directly instead of the obsolete Cron.GetDescription.
  3. If you need 6-field support, validate and parse with NCrontab or CronExpressionDescriptor directly.

Example fix

// before
Cron.GetDescription("0 0 12 * * ? *"); // 7 fields (quartz)

// after
Cron.GetDescription("0 12 * * *"); // 5 fields (standard cron)
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValid5FieldCron(string expr)
{
    var parts = expr.Split(' ');
    return parts.Length == 5;
}

if (!IsValid5FieldCron(cronExpr))
    throw new ArgumentException("Use a 5-field cron expression.");

Try / catch

try { Cron.GetDescription(expr); }
catch (InvalidCastException) { /* fall back to raw expr or NCrontab */ }

Prevention

When it happens

Trigger: Calling Cron.GetDescription with an expression that does not have exactly 5 space-separated fields, e.g. a 6-field expression with seconds, a quartz-style expression, or a macro like '@daily'.

Common situations: Using a 6-field cron expression (with seconds) from Quartz.NET config; passing a special macro (@hourly); copying an expression from a system crontab that uses ranges/lists that this naive validator rejects at the count check.

Related errors


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