JeffreySu/WeiXinMPSDK · error · InvalidOperationException

注册委托数量已达到上限 。

Error message

注册委托数量已达到上限 {_maximumCount}。

What it means

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.

Solutions

  1. Call Container.CheckRegistered(appId) first and skip re-registration if already registered.
  2. Increase MaximumCount if you genuinely need more accounts.
  3. Use the indexer RegisterFuncCollection[key] = func to overwrite an existing key instead of Add.
  4. Unregister obsolete accounts before adding new ones.

Example fix

// before
Container.RegisterFuncCollection.Add(appId, () => Container.GetBagAsync(appId));
// after
if (!Container.CheckRegistered(appId))
{
    Container.RegisterFuncCollection.Add(appId, () => Container.GetBagAsync(appId));
}
Defensive patterns

Strategy: validation

Validate before calling

var coll = container.RegisterFuncCollection;
if (!Container.CheckRegistered(appId) && coll.Count >= coll.MaximumCount)
    throw new InvalidOperationException("No free registration slots");

Try / catch

try { coll.Add(appId, func); }
catch (InvalidOperationException ex) when (ex.Message.Contains("上限"))
{ logger.LogWarning(ex, "Add rejected: capacity full"); }

Prevention

When it happens

Trigger: 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).

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

Related errors


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

Appendix: source

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

        /// </summary>
        public new int Count
        {
            get
            {
                lock (_capacityLock)
                {
                    return base.Count;
                }
            }
        }

        public new void Add(string key, Func<Task<TBag>> value)
        {
            lock (_capacityLock)
            {
                if (base.Count >= _maximumCount)
                {
                    throw new InvalidOperationException($"注册委托数量已达到上限 {_maximumCount}。");
                }

                base.Add(key, value);
            }
        }

        public new bool Remove(string key)
        {
            lock (_capacityLock)
            {
                return base.Remove(key);
            }
        }

        public new bool ContainsKey(string key)
        {
            lock (_capacityLock)
            {

View on GitHub (pinned to be573f6f94)