JeffreySu/WeiXinMPSDK · error · InvalidOperationException

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

Error message

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

What it means

SetRegistrationCallback stores one callback per bag keyed by shortKey. When registering a NEW key while RegisterFuncCollection.Count already equals _maximumRegistrationCallbackCount, it throws InvalidOperationException, telling you to unregister stale accounts or raise the limit.

Solutions

  1. Increase the container's MaximumRegistrationCallbackCount before registering (set it to expected account count with headroom).
  2. Remove/unregister bags for accounts no longer in use to free slots.
  3. Audit for duplicate registrations of the same appId that consume slots unnecessarily.
  4. If you genuinely need unbounded accounts, set the cap high enough at startup (it must exceed total registrations).

Example fix

// before
AccessTokenContainer.Register(appId, secret); // throws when cap reached
// after
if (expectedAccounts > currentCap)
{
    AccessTokenContainer.MaximumRegistrationCallbackCount = expectedAccounts * 2;
}
AccessTokenContainer.Register(appId, secret);
Defensive patterns

Strategy: try-catch

Validate before calling

// before registering, ensure capacity
if (expectedAccounts >= configuredCap)
    MyContainer.MaximumRegistrationCallbackCount = expectedAccounts * 2;

Try / catch

try
{
    AccessTokenContainer.Register(appId, secret);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("上限"))
{
    Log("Registration cap reached; raising limit and retrying.");
    AccessTokenContainer.MaximumRegistrationCallbackCount *= 2;
    AccessTokenContainer.Register(appId, secret);
}

Prevention

When it happens

Trigger: Registering more distinct accounts/appIds (each triggering SetRegistrationCallback with a new shortKey) than MaximumRegistrationCallbackCount allows; happens inside container registration (e.g. AccessTokenContainer.Register) once the cap is hit.

Common situations: Multi-tenant systems onboarding more WeChat accounts than the default cap; long-running services accumulating registrations without cleanup; defaults too low after migrating many tenants.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainer.cs:257

        /// </summary>
        protected static void SetRegistrationCallback(string shortKey, Func<Task<TBag>> registerFunc)
        {
            if (shortKey == null)
            {
                throw new ArgumentNullException(nameof(shortKey));
            }

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

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

                RegisterFuncCollection[shortKey] = registerFunc;
            }
        }

        /// <summary>注销指定账号,同时移除自动重注册委托和缓存项。</summary>
        public static bool Unregister(string shortKey)
        {
            if (shortKey == null)
            {
                throw new ArgumentNullException(nameof(shortKey));
            }

            var callbackRemoved = RegisterFuncCollection.TryRemove(shortKey, out _);
            RemoveFromCache(shortKey);
            return callbackRemoved;

View on GitHub (pinned to be573f6f94)