{"record":{"id":"b46d5c67e34147b9","repo":"abpframework/abp","slug":"period-must-be-greater-than-0-when-provided-given","errorCode":null,"errorMessage":"Period must be greater than 0 when provided. Given value: {Period.Value}.","messagePattern":"Period must be greater than 0 when provided\\. Given value: (.+?)\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/DynamicBackgroundWorkerSchedule.cs","lineNumber":17,"sourceCode":"using System;\n\nnamespace Volo.Abp.BackgroundWorkers;\n\npublic class DynamicBackgroundWorkerSchedule\n{\n    public const int DefaultPeriod = 60000;\n\n    public int? Period { get; set; }\n\n    public string? CronExpression { get; set; }\n\n    public virtual void Validate()\n    {\n        if (Period.HasValue && Period.Value <= 0)\n        {\n            throw new ArgumentException(\n                $\"Period must be greater than 0 when provided. Given value: {Period.Value}.\",\n                nameof(Period));\n        }\n\n        if (Period == null && string.IsNullOrWhiteSpace(CronExpression))\n        {\n            throw new ArgumentException(\n                \"At least one of 'Period' or 'CronExpression' must be set.\");\n        }\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/abpframework/abp/blob/7ed43b1931b9df46a50c0c59148a18645641d0df/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/DynamicBackgroundWorkerSchedule.cs#L1-L29","documentation":"Thrown by DynamicBackgroundWorkerSchedule.Validate when the Period property has a value but that value is zero or negative. A dynamic worker needs a positive interval to schedule work; a non-positive period would cause an immediate infinite loop or no firing. Validate is called by the dynamic worker manager's AddAsync and UpdateScheduleAsync before any work is scheduled.","triggerScenarios":"Constructing a DynamicBackgroundWorkerSchedule with Period = 0 or a negative integer and passing it to IDynamicBackgroundWorkerManager.AddAsync or UpdateScheduleAsync (which call schedule.Validate()). Also thrown if Validate() is called manually.","commonSituations":"Reading the period from configuration (appsettings/ISettingProvider) where the key is missing and defaults to 0. Computing a period from a difference that evaluates to zero or negative (e.g. a 'run in N minutes' computed from a past timestamp). Accidentally setting Period instead of CronExpression with a small value meant as seconds.","solutions":["Set Period to a positive value in milliseconds (e.g. 60000 for 1 minute).","Validate configuration before constructing the schedule and fall back to DynamicBackgroundWorkerSchedule.DefaultPeriod (60000) when the configured value is invalid.","If you meant to use cron, leave Period null and set CronExpression instead (and use Hangfire/Quartz)."],"exampleFix":"// before — period read from config, may be 0\nvar period = configuration.GetValue<int>(\"WorkerPeriod\");\nawait manager.AddAsync(\"worker\",\n    new DynamicBackgroundWorkerSchedule { Period = period }, handler);\n\n// after — sanitize the configured value\nvar period = configuration.GetValue<int>(\"WorkerPeriod\");\nif (period <= 0) period = DynamicBackgroundWorkerSchedule.DefaultPeriod;\nawait manager.AddAsync(\"worker\",\n    new DynamicBackgroundWorkerSchedule { Period = period }, handler);","handlingStrategy":"validation","validationCode":"var period = configuration.GetValue<int?>(\"WorkerPeriod\");\nif (period.HasValue && period.Value <= 0)\n{\n    throw new ArgumentOutOfRangeException(nameof(period), \"WorkerPeriod must be greater than 0.\");\n}\nvar schedule = new DynamicBackgroundWorkerSchedule { Period = period };","typeGuard":"static bool IsValidPeriod(int? period) => !period.HasValue || period.Value > 0;","tryCatchPattern":null,"preventionTips":["Validate configured period values at startup and fall back to DynamicBackgroundWorkerSchedule.DefaultPeriod when invalid.","Treat a missing configuration value as 'unset' (null) rather than 0 so you can default it correctly.","Add unit tests for schedule.Validate() covering zero, negative, and null inputs."],"tags":["background-workers","dynamic-workers","argument-validation","scheduling","configuration"],"backgroundTag":null,"analyzedSha":"7ed43b1931b9df46a50c0c59148a18645641d0df","analyzedAt":"2026-08-13T16:26:11.351Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}