{"record":{"id":"c82a1be151229fdd","repo":"JeffreySu/WeiXinMPSDK","slug":"maximumcount-maximumcount","errorCode":null,"errorMessage":"注册委托数量已达到上限 {_maximumCount}，请先注销不再使用的账号或提高 MaximumCount。","messagePattern":"注册委托数量已达到上限 (.+?)，请先注销不再使用的账号或提高 MaximumCount。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs","lineNumber":89,"sourceCode":"                }\n            }\n            set\n            {\n                if (key == null)\n                {\n                    throw new ArgumentNullException(nameof(key));\n                }\n\n                if (value == null)\n                {\n                    throw new ArgumentNullException(nameof(value));\n                }\n\n                lock (_capacityLock)\n                {\n                    if (!ContainsKey(key) && Count >= _maximumCount)\n                    {\n                        throw new InvalidOperationException($\"注册委托数量已达到上限 {_maximumCount}，请先注销不再使用的账号或提高 MaximumCount。\");\n                    }\n\n                    base[key] = value;\n                }\n            }\n        }\n\n        /// <summary>\n        /// 获取当前注册委托数量。\n        /// </summary>\n        public new int Count\n        {\n            get\n            {\n                lock (_capacityLock)\n                {\n                    return base.Count;\n                }","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs#L71-L107","documentation":"The indexer setter of BaseContainerRegisterFuncCollection enforces a registration cap: adding a NEW key when Count has already reached _maximumCount throws InvalidOperationException. This prevents unbounded memory growth from registering more WeChat accounts/containers than licensed or configured. Existing keys can still be overwritten.","triggerScenarios":"Registering a new appId via Container.Register / RegisterFuncCollection[key] = func when the number of already-registered delegates equals MaximumCount.","commonSituations":"Registering many WeChat公众号/企业号 accounts in a long-running app that accumulates registrations; a memory-limit configuration set lower than the number of tenants; leaked registrations never unregistered, gradually filling capacity.","solutions":["Increase MaximumCount (e.g. SenparcSetting or code) to accommodate all accounts.","Call Container.Unregister(appId) for accounts no longer used before registering new ones.","Cache registration results so the app does not re-register repeatedly and exhaust slots.","Catch InvalidOperationException around Register to log and prompt for capacity increase."],"exampleFix":"// before\nContainer.Register(appId, appSecret); // throws when full\n// after\nif (!Container.CheckRegistered(appId))\n{\n    if (Container.RegisterFuncCollection.Count >= Container.RegisterFuncCollection.MaximumCount)\n        Container.Unregister(oldestUnusedAppId);\n}\nContainer.Register(appId, appSecret);","handlingStrategy":"try-catch","validationCode":"if (!container.CheckRegistered(appId) &&\n    container.RegisterFuncCollection.Count >= container.RegisterFuncCollection.MaximumCount)\n{\n    // free a slot or increase capacity before registering\n}","typeGuard":null,"tryCatchPattern":"try { Container.Register(appId, appSecret); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"上限\"))\n{ logger.LogWarning(ex, \"Registration capacity reached for {AppId}\", appId); }","preventionTips":["Check CheckRegistered before every Register","Unregister unused accounts on tenant removal","Set MaximumCount generously above tenant count"],"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"}