{"record":{"id":"31394fd752824717","repo":"JeffreySu/WeiXinMPSDK","slug":"count-value","errorCode":null,"errorMessage":"当前已有 {Count} 个注册委托，不能把容量降低到 {value}。","messagePattern":"当前已有 (.+?) 个注册委托，不能把容量降低到 (.+?)。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs","lineNumber":53,"sourceCode":"\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]\n        {\n            get\n            {\n                lock (_capacityLock)\n                {\n                    return base[key];\n                }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs#L35-L71","documentation":"When shrinking BaseContainerRegisterFuncCollection.MaximumCount, the setter refuses to set the new capacity below the number of registration delegates already stored (Count). It throws InvalidOperationException to avoid orphaning existing registered accounts. You must remove/unregister delegates first or keep capacity at or above the current count.","triggerScenarios":"Assigning MaximumCount = N when the collection already holds more than N registered delegates, e.g. lowering the limit after registering several WeChat accounts.","commonSituations":"Dynamically recomputing capacity from a shrunken license/account list; admin lowering an app setting while accounts remain registered; app restart re-registering accounts before the smaller cap is applied.","solutions":["Raise MaximumCount instead of lowering it, or lower it only to a value >= the current Count.","Unregister/remove unused account delegates (Container.Unregister) until Count <= new value, then set the cap.","Check the current Count before assigning and clamp the new value accordingly.","Serialize configuration changes so re-registration does not push Count above the new cap."],"exampleFix":"// before\ncontainer.RegisterFuncCollection.MaximumCount = 5; // 8 accounts registered\n// after\nvar coll = container.RegisterFuncCollection;\nif (coll.Count > 5) { /* unregister 3 unused accounts first */ }\ncoll.MaximumCount = Math.Max(5, coll.Count);","handlingStrategy":"validation","validationCode":"var coll = container.RegisterFuncCollection;\nif (newCapacity < coll.Count)\n    newCapacity = coll.Count; // or unregister first\ncoll.MaximumCount = newCapacity;","typeGuard":null,"tryCatchPattern":"try { coll.MaximumCount = value; }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"Cannot shrink below {N} registered delegates\", coll.Count); }","preventionTips":["Only lower capacity after unregistering accounts","Recompute capacity from live Count, not stale config","Serialize config changes with registration logic"],"tags":["csharp","weixin","container","state"],"backgroundTag":"invalid-state-transition","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"}