JeffreySu/WeiXinMPSDK · error · InvalidOperationException

当前已有 个注册委托,不能把容量降低到 。

Error message

当前已有 {Count} 个注册委托,不能把容量降低到 {value}。

What it means

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.

Solutions

  1. Raise MaximumCount instead of lowering it, or lower it only to a value >= the current Count.
  2. Unregister/remove unused account delegates (Container.Unregister) until Count <= new value, then set the cap.
  3. Check the current Count before assigning and clamp the new value accordingly.
  4. Serialize configuration changes so re-registration does not push Count above the new cap.

Example fix

// before
container.RegisterFuncCollection.MaximumCount = 5; // 8 accounts registered
// after
var coll = container.RegisterFuncCollection;
if (coll.Count > 5) { /* unregister 3 unused accounts first */ }
coll.MaximumCount = Math.Max(5, coll.Count);
Defensive patterns

Strategy: validation

Validate before calling

var coll = container.RegisterFuncCollection;
if (newCapacity < coll.Count)
    newCapacity = coll.Count; // or unregister first
coll.MaximumCount = newCapacity;

Try / catch

try { coll.MaximumCount = value; }
catch (InvalidOperationException ex) { logger.LogError(ex, "Cannot shrink below {N} registered delegates", coll.Count); }

Prevention

When it happens

Trigger: Assigning MaximumCount = N when the collection already holds more than N registered delegates, e.g. lowering the limit after registering several WeChat accounts.

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

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

        /// <summary>
        /// 最多保留的自动重注册委托数量。必须大于 0,默认为 10000。
        /// </summary>
        public int MaximumCount
        {
            get => _maximumCount;
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), "注册委托容量必须大于 0。");
                }

                lock (_capacityLock)
                {
                    if (value < Count)
                    {
                        throw new InvalidOperationException($"当前已有 {Count} 个注册委托,不能把容量降低到 {value}。");
                    }

                    _maximumCount = value;
                }
            }
        }

        /// <summary>
        /// 使用容量检查设置注册委托;替换已有键不占用新容量。
        /// </summary>
        public new Func<Task<TBag>> this[string key]
        {
            get
            {
                lock (_capacityLock)
                {
                    return base[key];
                }

View on GitHub (pinned to be573f6f94)