JeffreySu/WeiXinMPSDK · error · InvalidOperationException

注册委托数量已达到上限 ,请先注销不再使用的账号或提高 MaximumCount。

Error message

注册委托数量已达到上限 {_maximumCount},请先注销不再使用的账号或提高 MaximumCount。

What it means

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.

Solutions

  1. Increase MaximumCount (e.g. SenparcSetting or code) to accommodate all accounts.
  2. Call Container.Unregister(appId) for accounts no longer used before registering new ones.
  3. Cache registration results so the app does not re-register repeatedly and exhaust slots.
  4. Catch InvalidOperationException around Register to log and prompt for capacity increase.

Example fix

// before
Container.Register(appId, appSecret); // throws when full
// after
if (!Container.CheckRegistered(appId))
{
    if (Container.RegisterFuncCollection.Count >= Container.RegisterFuncCollection.MaximumCount)
        Container.Unregister(oldestUnusedAppId);
}
Container.Register(appId, appSecret);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!container.CheckRegistered(appId) &&
    container.RegisterFuncCollection.Count >= container.RegisterFuncCollection.MaximumCount)
{
    // free a slot or increase capacity before registering
}

Try / catch

try { Container.Register(appId, appSecret); }
catch (InvalidOperationException ex) when (ex.Message.Contains("上限"))
{ logger.LogWarning(ex, "Registration capacity reached for {AppId}", appId); }

Prevention

When it happens

Trigger: Registering a new appId via Container.Register / RegisterFuncCollection[key] = func when the number of already-registered delegates equals MaximumCount.

Common situations: 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.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/c82a1be151229fdd. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainerRegisterFuncCollection.cs:89

                }
            }
            set
            {
                if (key == null)
                {
                    throw new ArgumentNullException(nameof(key));
                }

                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value));
                }

                lock (_capacityLock)
                {
                    if (!ContainsKey(key) && Count >= _maximumCount)
                    {
                        throw new InvalidOperationException($"注册委托数量已达到上限 {_maximumCount},请先注销不再使用的账号或提高 MaximumCount。");
                    }

                    base[key] = value;
                }
            }
        }

        /// <summary>
        /// 获取当前注册委托数量。
        /// </summary>
        public new int Count
        {
            get
            {
                lock (_capacityLock)
                {
                    return base.Count;
                }

View on GitHub (pinned to be573f6f94)