JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

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

Error message

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

What it means

BaseContainer exposes MaximumRegistrationCallbackCount, the cap on how many registration callbacks (one per container bag/account) can be stored. The setter validates that the new value is > 0 and throws ArgumentOutOfRangeException for zero or negative values.

Solutions

  1. Set MaximumRegistrationCallbackCount to a positive integer sized to your account count (e.g. 100+).
  2. If the value comes from config, guard: only assign when the parsed value > 0; otherwise keep the library default.
  3. Use int.TryParse with an explicit positive check instead of defaulting to 0 on parse failure.
  4. Do not try to 'disable' the cap with 0 — the property only accepts positive values.

Example fix

// before
var limit = int.Parse(config["MaxRegistrations"]); // 0 when key missing
MyContainer.MaximumRegistrationCallbackCount = limit; // throws
// after
if (int.TryParse(config["MaxRegistrations"], out var limit) && limit > 0)
{
    MyContainer.MaximumRegistrationCallbackCount = limit;
} // else keep library default
Defensive patterns

Strategy: validation

Validate before calling

if (value <= 0)
    throw new InvalidOperationException("MaximumRegistrationCallbackCount must be positive.");
MyContainer.MaximumRegistrationCallbackCount = value;

Try / catch

try
{
    MyContainer.MaximumRegistrationCallbackCount = parsedLimit;
}
catch (ArgumentOutOfRangeException ex)
{
    Log($"Invalid limit {parsedLimit}: {ex.Message}; keeping default.");
}

Prevention

When it happens

Trigger: Setting MaximumRegistrationCallbackCount (static property on a BaseContainer-derived container) to 0 or a negative number, typically from config parsing that yields 0 as a default when the config key is missing.

Common situations: Reading the limit from appsettings with int.Parse/TryParse defaulting to 0; misconfigured environment variables; code assuming a boolean-like 'disable limit' semantic by passing 0.

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/25858f14cc249ea8. Report an issue: GitHub.

Appendix: source

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

        /// <summary>当前保留的自动重注册委托数量。</summary>
        public static int RegistrationCallbackCount => RegisterFuncCollection.Count;

        /// <summary>自动重注册委托容量上限。默认 10000。</summary>
        public static int MaximumRegistrationCallbackCount
        {
            get
            {
                lock (RegistrationCallbackSyncRoot)
                {
                    return _maximumRegistrationCallbackCount;
                }
            }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), "注册委托容量必须大于 0。");
                }

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

                    _maximumRegistrationCallbackCount = value;
                }
            }
        }

        /// <summary>
        /// 为 SDK 内部注册自动刷新委托,同时执行容量检查。
        /// 保留 <see cref="RegisterFuncCollection"/> 的历史类型,以兼容外部派生容器。
        /// </summary>

View on GitHub (pinned to be573f6f94)