{"record":{"id":"62dd873198f99b1f","repo":"HangfireIO/Hangfire","slug":"attempts-value-must-be-equal-or-greater-than-zero","errorCode":null,"errorMessage":"Attempts value must be equal or greater than zero.","messagePattern":"Attempts value must be equal or greater than zero\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.Core/AutomaticRetryAttribute.cs","lineNumber":131,"sourceCode":"            DelayInSecondsByAttemptFunc = DefaultDelayInSecondsByAttemptFunc;\n            LogEvents = true;\n            OnAttemptsExceeded = AttemptsExceededAction.Fail;\n            Order = 20;\n        }\n\n        /// <summary>\n        /// Gets or sets the maximum number of automatic retry attempts.\n        /// </summary>\n        /// <value>Any non-negative number.</value>\n        /// <exception cref=\"ArgumentOutOfRangeException\">The value in a set operation is less than zero.</exception>\n        public int Attempts\n        {\n            get { lock (_lockObject) { return _attempts; } }\n            set\n            {\n                if (value < 0)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(value), @\"Attempts value must be equal or greater than zero.\");\n                }\n\n                lock (_lockObject)\n                {\n                    _attempts = value;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets the delays between attempts.\n        /// </summary>\n        /// <value>An array of non-negative numbers.</value>\n        /// <exception cref=\"ArgumentNullException\">The value in a set operation is null.</exception>\n        /// <exception cref=\"ArgumentException\">The value contain one or more negative numbers.</exception>\n        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]\n        public int[] DelaysInSeconds\n        {","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/AutomaticRetryAttribute.cs#L113-L149","documentation":"The Attempts property on AutomaticRetryAttribute (and other retry attributes that reuse it) represents the maximum automatic retry count and must be non-negative. Setting it to a negative number throws ArgumentOutOfRangeException immediately at assignment time, before any job runs. This is input validation on a configuration property, not a runtime job-execution failure.","triggerScenarios":"Setting [AutomaticRetry(Attempts = -1)] (or assigning Attempts = -5) on a job method or globally via GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = -1 }). Any value < 0 triggers the guard.","commonSituations":"Misunderstanding Attempts as a retry count offset (setting -1 intending 'infinite' or 'none'); loading Attempts from configuration that parses an empty/invalid string to a negative; copying a value meant for DelaysInSeconds.","solutions":["Set Attempts to 0 to disable retries, or a positive integer for the number of attempts.","If you intended infinite retries, that is not supported — use a large finite number or implement a custom retry filter.","Validate config-driven values before assigning: clamp negatives to 0 in your configuration loader."],"exampleFix":"// before\n[AutomaticRetry(Attempts = -1)]\npublic void Run() { }\n\n// after\n[AutomaticRetry(Attempts = 0)] // disable retries\npublic void Run() { }","handlingStrategy":"validation","validationCode":"int attempts = LoadFromConfig();\nif (attempts < 0) attempts = 0; // clamp before assigning\nattribute.Attempts = attempts;","typeGuard":"static bool IsValidAttempts(int value) => value >= 0;","tryCatchPattern":null,"preventionTips":["Validate config-derived values before assigning to Attempts.","Use 0 to disable retries; never use negatives.","Add a unit test asserting Attempts assignment accepts 0 and rejects -1."],"tags":["configuration","validation","retry","argumentoutofrangeexception","attribute"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}