JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException
单次拉取条数必须介于 1 和 1000 之间。
Error message
单次拉取条数必须介于 1 和 1000 之间。
What it means
MsgAuditFinanceClient.GetChatData pulls session audit data from the WeCom Finance SDK. WeCom officially allows 1–1000 records per pull, so the client rejects limit==0 or limit>1000 with ArgumentOutOfRangeException before touching the native SDK.
Solutions
- Clamp the limit: limit = Math.Clamp(limit, 1, 1000)
- Fix paging logic so the final call uses a positive limit and stops when nothing remains
- Keep 1000 as the default; only pass explicit limits within 1–1000
Example fix
// before client.GetChatData(sequence, 5000); // after var clamped = Math.Clamp(limit, 1, 1000); client.GetChatData(sequence, clamped);
Defensive patterns
Strategy: validation
Validate before calling
if (limit == 0 || limit > 1000) throw new ArgumentException("limit must be 1..1000", nameof(limit));
client.GetChatData(sequence, limit); Type guard
static bool IsValidLimit(uint limit) => limit is >= 1 and <= 1000;
Try / catch
try { var data = client.GetChatData(seq, limit); } catch (ArgumentOutOfRangeException ex) { log.Error("limit out of range: {0}", ex.ParamName); } Prevention
- Clamp computed page sizes with Math.Clamp(limit, 1, 1000)
- Break loops when the API returns fewer records than requested
- Never reuse limits from other APIs with different caps
When it happens
Trigger: Calling GetChatData(sequence, limit) with limit = 0, an unbounded value (e.g. int.MaxValue cast to uint), or a computed limit like remaining-count that overflows the allowed range.
Common situations: Using a paging loop where the last-page size calculation yields 0; hardcoding a large batch size for 'fewer calls'; copying a limit from another API with different caps.
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/db3ba9d88b772c21.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/MsgAudit/MsgAuditFinanceClient.cs:84
{
ReleaseResources(false);
throw;
}
}
/// <summary>
/// 拉取加密会话记录。
/// </summary>
/// <param name="sequence">起始消息序号;返回 sequence 之后的消息,首次调用传 0。</param>
/// <param name="limit">单次拉取条数,必须介于 1 和 1000 之间。</param>
/// <returns>包含加密随机密钥、消息密文和完整原始 JSON 的强类型结果。</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="limit"/> 不在官方允许范围内。</exception>
/// <exception cref="MsgAuditFinanceException">原生 SDK 返回非零错误码。</exception>
public MsgAuditFinanceChatDataResult GetChatData(ulong sequence, uint limit = 1000)
{
if (limit == 0 || limit > 1000)
{
throw new ArgumentOutOfRangeException(nameof(limit), "单次拉取条数必须介于 1 和 1000 之间。");
}
lock (_syncRoot)
{
ThrowIfDisposed();
var slice = _nativeApi.NewSlice();
EnsureNativeHandle(slice, "NewSlice");
try
{
var errorCode = _nativeApi.GetChatData(_sdk, sequence, limit, _proxy,
_proxyPassword, _timeoutSeconds, slice);
ThrowIfNativeError(errorCode, "GetChatData");
var json = _nativeApi.GetSliceContent(slice);
if (string.IsNullOrWhiteSpace(json))
{
throw new InvalidDataException("Finance SDK 返回的会话内容为空。");
}View on GitHub (pinned to be573f6f94)