JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

注册委托容量必须大于 0。

Error message

注册委托容量必须大于 0。

What it means

BaseContainerRegisterFuncCollection.MaximumCount is a bounded capacity for how many account-registration delegate functions can be stored. The setter throws ArgumentOutOfRangeException when you assign a value <= 0, because a non-positive capacity would make the collection unusable. It is a defensive check on an invalid configuration value.

Solutions

  1. Set MaximumCount to a positive value at least as large as the number of accounts you will register.
  2. If the value comes from config, validate it is > 0 before assigning and fall back to a sane default.
  3. Register accounts first, then raise MaximumCount if needed.
  4. Wrap the assignment in a range check or clamp: Math.Max(1, configuredValue).

Example fix

// before
container.RegisterFuncCollection.MaximumCount = int.Parse(config["maxAccounts"]); // 0 from empty config
// after
var max = int.TryParse(config["maxAccounts"], out var m) ? m : 100;
container.RegisterFuncCollection.MaximumCount = Math.Max(1, max);
Defensive patterns

Strategy: validation

Validate before calling

if (configuredCapacity <= 0)
    throw new InvalidOperationException("MaximumCount must be positive");
container.RegisterFuncCollection.MaximumCount = configuredCapacity;

Try / catch

try { coll.MaximumCount = value; }
catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, "Invalid capacity {V}", value); }

Prevention

When it happens

Trigger: Setting container.RegisterFuncCollection.MaximumCount = 0 or a negative number, e.g. reading the limit from config where it defaulted to 0.

Common situations: Config file missing the capacity setting so it defaults to 0; computing the limit from a count of accounts that is currently 0; copying sample code that sets MaximumCount explicitly.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        private readonly object _capacityLock = new object();
        private int _maximumCount = 10000;

        public BaseContainerRegisterFuncCollection()
            : base(StringComparer.OrdinalIgnoreCase)
        {
        }

        /// <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]

View on GitHub (pinned to be573f6f94)