{"record":{"id":"2ea76d01f462c561","repo":"dotnetcore/CAP","slug":"worker-id-can-t-be-greater-than-maxworkerid-or-less-than-0","errorCode":null,"errorMessage":"worker Id can't be greater than {MaxWorkerId} or less than 0","messagePattern":"worker Id can't be greater than (.+?) or less than 0","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP/Internal/ISnowflakeId.Default.cs","lineNumber":118,"sourceCode":"    {\n        var currentWithSequence = ++_timestampAndSequence;\n        var current = currentWithSequence >> SequenceBits;\n        var newest = GetNewestTimestamp();\n\n        if (current >= newest) Thread.Sleep(5);\n    }\n\n    /// <summary>\n    /// Common method for initializing <see cref=\"SnowflakeId\"/>\n    /// </summary>\n    /// <param name=\"workerId\"></param>\n    /// <exception cref=\"ArgumentException\"></exception>\n    private void Initialize(long workerId)\n    {\n        InitTimestampAndSequence();\n        // sanity check for workerId\n        if (workerId is > MaxWorkerId or < 0)\n            throw new ArgumentException($\"worker Id can't be greater than {MaxWorkerId} or less than 0\");\n\n        WorkerId = workerId << (TimestampBits + SequenceBits);\n    }\n\n    /// <summary>\n    /// get newest timestamp relative to twepoch\n    /// </summary>\n    /// <returns></returns>\n    private static long GetNewestTimestamp()\n    {\n        return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - Twepoch;\n    }\n}\n\ninternal static class Util\n{\n    /// <summary>\n    /// auto generate workerId, try using mac first, if failed, then randomly generate one","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP/Internal/ISnowflakeId.Default.cs#L100-L136","documentation":"An argument sanity check in SnowflakeId's Initialize: the workerId supplied (from SnowflakeIdOptions or auto-detection) falls outside the legal range 0..MaxWorkerId (10 bits, i.e. 0–1023). Worker ids must uniquely identify each process within that bit budget; anything larger would collide with the timestamp/sequence bits, so ArgumentException is thrown during id-generator construction.","triggerScenarios":"Thrown at src/DotNetCore.CAP/Internal/ISnowflakeId.Default.cs:118 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Set CAP.SnowflakeId.WorkerId (SnowflakeIdOptions) to a value between 0 and 1023 that is unique per running instance.","For multi-node deployments, assign worker ids via configuration/deployment environment or a coordination service rather than hard-coding the same value everywhere.","Leave WorkerId unset to let CAP auto-generate one from the machine's MAC address or a random value."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb","analyzedAt":"2026-09-14T14:46:34.677Z","contentChangedAt":"2026-09-14T14:46:34.677Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}