JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException
请求体上限必须大于 0。
Error message
请求体上限必须大于 0。
What it means
ReadBodyAsync accepts an optional maxBodyBytes limit to cap how many bytes of the notification body are buffered; a value of 0 or negative is meaningless and triggers ArgumentOutOfRangeException("请求体上限必须大于 0。").
Solutions
- Pass a positive value (e.g. 1 MB = 1048576) or pass null to disable the limit
- Validate configured max body size at startup and reject <= 0
- Fix arithmetic that computes maxBodyBytes dynamically
- Use int.MaxValue only intentionally — prefer null for "no limit"
Example fix
// before var body = await ReadBodyAsync(httpContext, 0, ct); // after var body = await ReadBodyAsync(httpContext, 1024 * 1024, ct); // or null for no limit
Defensive patterns
Strategy: validation
Validate before calling
if (maxBodyBytes is <= 0) throw new ArgumentException("maxBodyBytes 必须为正数或 null。", nameof(maxBodyBytes)); Type guard
bool IsValidBodyLimit(int? n) => n is null || n > 0;
Try / catch
try { var body = await ReadBodyAsync(httpContext, maxBodyBytes, ct); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "maxBodyBytes") { logger.LogError(ex, "maxBodyBytes 配置非法"); } Prevention
- Centralize the body-size constant instead of hard-coding per call
- Validate limits from config at startup
- Use null, not 0, for "no limit"
When it happens
Trigger: Calling ReadBodyAsync / the notify handler with maxBodyBytes set to 0 or a negative integer.
Common situations: Misreading the parameter as "unlimited" (passing 0), computing the limit from config that defaulted to 0, or off-by-one arithmetic.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/28b1138cb2b347fd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs:166
public static async Task<TenPayNotifyHandler> CreateAsync(
HttpContext httpContext,
ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null,
int maxBodyBytes = DefaultMaxBodyBytes,
CancellationToken cancellationToken = default)
{
var notificationBody = await ReadBodyAsync(httpContext, maxBodyBytes, cancellationToken).ConfigureAwait(false);
return new TenPayNotifyHandler(httpContext, senparcWeixinSettingForTenpayV3, notificationBody);
}
private static async Task<NotificationBody> ReadBodyAsync(
HttpContext httpContext,
int? maxBodyBytes,
CancellationToken cancellationToken)
{
_ = httpContext ?? throw new ArgumentNullException(nameof(httpContext));
if (maxBodyBytes.HasValue && maxBodyBytes.Value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxBodyBytes), "请求体上限必须大于 0。");
}
var request = httpContext.Request;
if (request.Method != "POST" && request.Method != "PUT" && request.Method != "PATCH")
{
return NotificationBody.Empty;
}
if (maxBodyBytes.HasValue && request.ContentLength > maxBodyBytes.Value)
{
throw new InvalidDataException($"微信支付通知请求体超过允许上限 {maxBodyBytes} 字节。");
}
if (maxBodyBytes.HasValue)
{
request.EnableBuffering(bufferThreshold: 30 * 1024, bufferLimit: maxBodyBytes.Value);
}
elseView on GitHub (pinned to be573f6f94)