{"record":{"id":"252344f7f6d1fbfd","repo":"HangfireIO/Hangfire","slug":"invalid-cron-expression","errorCode":null,"errorMessage":"Invalid Cron Expression","messagePattern":"Invalid Cron Expression","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"warning","filePath":"src/Hangfire.Core/Cron.cs","lineNumber":293,"sourceCode":"        public static string MonthInterval(int interval)\n        {\n            return $\"0 0 1 */{interval} *\";\n        }\n\n#if FEATURE_CRONDESCRIPTOR\n        /// <summary>\n        /// Converts a Cron expression string into a description.\n        /// </summary>\n        /// <param name=\"cronExpression\">A Cron expression string.</param>\n        /// <returns>English description.</returns>\n        [Obsolete(\"Please install `CronExpressionDescriptor` package manually and use it.\")]\n        public static string GetDescription(string cronExpression)\n        {\n            string[] expressionParts = cronExpression.Split(' ');\n\n            if (expressionParts.Length != 5)\n            {\n                throw new InvalidCastException(\"Invalid Cron Expression\");\n            }\n\n            foreach (string expressionPart in expressionParts)\n            {\n                int num;\n                if (!Int32.TryParse(expressionPart, out num) && expressionPart != \"*\")\n                {\n                    throw new InvalidCastException(\"Invalid Cron Expression\");\n                }\n            }\n\n            return CronExpressionDescriptor.ExpressionDescriptor.GetDescription(cronExpression);\n        }\n#endif\n    }\n}\n","sourceCodeStart":275,"sourceCodeEnd":310,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/Cron.cs#L275-L310","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Provide a standard 5-field cron expression (minute hour day month day-of-week).","Install and use the CronExpressionDescriptor package directly instead of the obsolete Cron.GetDescription.","If you need 6-field support, validate and parse with NCrontab or CronExpressionDescriptor directly."],"exampleFix":"// before\nCron.GetDescription(\"0 0 12 * * ? *\"); // 7 fields (quartz)\n\n// after\nCron.GetDescription(\"0 12 * * *\"); // 5 fields (standard cron)","handlingStrategy":"validation","validationCode":"static bool IsValid5FieldCron(string expr)\n{\n    var parts = expr.Split(' ');\n    return parts.Length == 5;\n}\n\nif (!IsValid5FieldCron(cronExpr))\n    throw new ArgumentException(\"Use a 5-field cron expression.\");","typeGuard":null,"tryCatchPattern":"try { Cron.GetDescription(expr); }\ncatch (InvalidCastException) { /* fall back to raw expr or NCrontab */ }","preventionTips":["Migrate off the obsolete Cron.GetDescription to the CronExpressionDescriptor package.","Validate cron field count (must be 5) before calling.","Prefer NCrontab for scheduling and validation."],"tags":["cron","validation","obsolete","invalidcast"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}