{"record":{"id":"ca847a2dfd4023e7","repo":"JeffreySu/WeiXinMPSDK","slug":"0-basecontainerregisterfunccollection","errorCode":null,"errorMessage":"注册委托容量必须大于 0。","messagePattern":"注册委托容量必须大于 0。","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs","lineNumber":46,"sourceCode":"        private readonly object _capacityLock = new object();\n        private int _maximumCount = 10000;\n\n        public BaseContainerRegisterFuncCollection()\n            : base(StringComparer.OrdinalIgnoreCase)\n        {\n        }\n\n        /// <summary>\n        /// 最多保留的自动重注册委托数量。必须大于 0，默认为 10000。\n        /// </summary>\n        public int MaximumCount\n        {\n            get => _maximumCount;\n            set\n            {\n                if (value <= 0)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(value), \"注册委托容量必须大于 0。\");\n                }\n\n                lock (_capacityLock)\n                {\n                    if (value < Count)\n                    {\n                        throw new InvalidOperationException($\"当前已有 {Count} 个注册委托，不能把容量降低到 {value}。\");\n                    }\n\n                    _maximumCount = value;\n                }\n            }\n        }\n\n        /// <summary>\n        /// 使用容量检查设置注册委托；替换已有键不占用新容量。\n        /// </summary>\n        public new Func<Task<TBag>> this[string key]","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs#L28-L64","documentation":"BaseContainerRegisterFuncCollection.MaximumCount is a bounded capacity for how many account-registration delegate functions can be stored. The setter throws ArgumentOutOfRangeException when you assign a value <= 0, because a non-positive capacity would make the collection unusable. It is a defensive check on an invalid configuration value.","triggerScenarios":"Setting container.RegisterFuncCollection.MaximumCount = 0 or a negative number, e.g. reading the limit from config where it defaulted to 0.","commonSituations":"Config file missing the capacity setting so it defaults to 0; computing the limit from a count of accounts that is currently 0; copying sample code that sets MaximumCount explicitly.","solutions":["Set MaximumCount to a positive value at least as large as the number of accounts you will register.","If the value comes from config, validate it is > 0 before assigning and fall back to a sane default.","Register accounts first, then raise MaximumCount if needed.","Wrap the assignment in a range check or clamp: Math.Max(1, configuredValue)."],"exampleFix":"// before\ncontainer.RegisterFuncCollection.MaximumCount = int.Parse(config[\"maxAccounts\"]); // 0 from empty config\n// after\nvar max = int.TryParse(config[\"maxAccounts\"], out var m) ? m : 100;\ncontainer.RegisterFuncCollection.MaximumCount = Math.Max(1, max);","handlingStrategy":"validation","validationCode":"if (configuredCapacity <= 0)\n    throw new InvalidOperationException(\"MaximumCount must be positive\");\ncontainer.RegisterFuncCollection.MaximumCount = configuredCapacity;","typeGuard":null,"tryCatchPattern":"try { coll.MaximumCount = value; }\ncatch (ArgumentOutOfRangeException ex) { logger.LogError(ex, \"Invalid capacity {V}\", value); }","preventionTips":["Clamp config-driven capacities with Math.Max(1, value)","Never assign MaximumCount from unvalidated config","Pick a default > expected account count"],"tags":["csharp","weixin","container","capacity"],"backgroundTag":"value-out-of-range","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}