{"record":{"id":"608c5c8f40fc3ce6","repo":"JeffreySu/WeiXinMPSDK","slug":"maximumcount","errorCode":null,"errorMessage":"注册委托数量已达到上限 {_maximumCount}。","messagePattern":"注册委托数量已达到上限 (.+?)。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs","lineNumber":117,"sourceCode":"        /// </summary>\n        public new int Count\n        {\n            get\n            {\n                lock (_capacityLock)\n                {\n                    return base.Count;\n                }\n            }\n        }\n\n        public new void Add(string key, Func<Task<TBag>> value)\n        {\n            lock (_capacityLock)\n            {\n                if (base.Count >= _maximumCount)\n                {\n                    throw new InvalidOperationException($\"注册委托数量已达到上限 {_maximumCount}。\");\n                }\n\n                base.Add(key, value);\n            }\n        }\n\n        public new bool Remove(string key)\n        {\n            lock (_capacityLock)\n            {\n                return base.Remove(key);\n            }\n        }\n\n        public new bool ContainsKey(string key)\n        {\n            lock (_capacityLock)\n            {","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs#L99-L135","documentation":"The Add method of BaseContainerRegisterFuncCollection throws InvalidOperationException when the collection already contains _maximumCount registration delegates. Unlike the indexer (which allows overwriting an existing key), Add always inserts a new entry, so any Add at capacity fails. It is the same cap enforcement as the indexer but hit through the Add API.","triggerScenarios":"Calling container.RegisterFuncCollection.Add(appId, func) (or a Register path using Add) when Count >= MaximumCount, even if the key already exists (Add does not overwrite).","commonSituations":"Application startup re-running registration code on every request or every recycle without checking CheckRegistered; registering more WeChat accounts than the configured limit; migrating code from old SDK versions without the capacity concept.","solutions":["Call Container.CheckRegistered(appId) first and skip re-registration if already registered.","Increase MaximumCount if you genuinely need more accounts.","Use the indexer RegisterFuncCollection[key] = func to overwrite an existing key instead of Add.","Unregister obsolete accounts before adding new ones."],"exampleFix":"// before\nContainer.RegisterFuncCollection.Add(appId, () => Container.GetBagAsync(appId));\n// after\nif (!Container.CheckRegistered(appId))\n{\n    Container.RegisterFuncCollection.Add(appId, () => Container.GetBagAsync(appId));\n}","handlingStrategy":"validation","validationCode":"var coll = container.RegisterFuncCollection;\nif (!Container.CheckRegistered(appId) && coll.Count >= coll.MaximumCount)\n    throw new InvalidOperationException(\"No free registration slots\");","typeGuard":null,"tryCatchPattern":"try { coll.Add(appId, func); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"上限\"))\n{ logger.LogWarning(ex, \"Add rejected: capacity full\"); }","preventionTips":["Prefer the indexer over Add to allow key overwrite","Guard registrations with CheckRegistered","Avoid re-running registration on every request"],"tags":["csharp","weixin","container","capacity"],"backgroundTag":"capacity-limit-exceeded","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"}