JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

超时时间必须大于 0,或使用 Timeout.Infinite。

Error message

超时时间必须大于 0,或使用 Timeout.Infinite。 

What it means

ArgumentOutOfRangeException thrown by TenPayApiRequest.GetHttpResponseMessageAsync when the timeOut parameter is <= 0 and not Timeout.Infinite. The library validates that a request timeout is a positive value or explicitly infinite.

Solutions

  1. Pass a positive timeout in milliseconds, or Timeout.Infinite (-1) for no timeout
  2. Fix Config.TIME_OUT configuration so it is > 0
  3. Clamp/validate user-supplied timeouts before calling

Example fix

// before
await request.GetAsync(url, timeOut: 0);
// after
await request.GetAsync(url, timeOut: Timeout.Infinite); // or e.g. 30000
Defensive patterns

Strategy: validation

Validate before calling

if (timeOut <= 0 && timeOut != Timeout.Infinite)
    throw new ArgumentOutOfRangeException(nameof(timeOut), timeOut, "timeOut 必须为正数或 Timeout.Infinite");

Try / catch

try { await request.GetAsync(url, timeOut: timeOut); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "timeOut") { timeOut = Config.TIME_OUT; /* retry with default */ }

Prevention

When it happens

Trigger: Calling any TenPay V3 API request method (Get/Post/etc.) that forwards timeOut with 0, a negative number, or an uninitialized/default-like value.

Common situations: Passing a config value that defaulted to 0 when Config.TIME_OUT was overridden with an invalid value; computing a timeout dynamically and getting a negative result.

Understand the failure class

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:216

            return GetHttpResponseMessageAsync(url, data, CancellationToken.None, HttpCompletionOption.ResponseContentRead, timeOut, requestMethod, checkDataNotNull);
        }

        /// <summary>
        /// 获取 HttpResponseMessage 对象,并支持调用方取消及流式响应。
        /// </summary>
        /// <returns>响应对象由调用方负责释放。</returns>
        public async Task<HttpResponseMessage> GetHttpResponseMessageAsync(
            string url,
            object data,
            CancellationToken cancellationToken,
            HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead,
            int timeOut = Config.TIME_OUT,
            ApiRequestMethod requestMethod = ApiRequestMethod.POST,
            bool checkDataNotNull = true)
        {
            if (timeOut <= 0 && timeOut != Timeout.Infinite)
            {
                throw new ArgumentOutOfRangeException(nameof(timeOut), "超时时间必须大于 0,或使用 Timeout.Infinite。 ");
            }

            using var request = new HttpRequestMessage(GetHttpMethod(requestMethod), url);

            switch (requestMethod)
            {
                case ApiRequestMethod.GET:
                case ApiRequestMethod.DELETE:
                    WeixinTrace.Log(url);
                    break;
                case ApiRequestMethod.POST:
                case ApiRequestMethod.PUT:
                case ApiRequestMethod.PATCH:
                    if (checkDataNotNull)
                    {
                        _ = data ?? throw new ArgumentNullException($"{nameof(data)} 不能为 null!");
                    }

View on GitHub (pinned to be573f6f94)