JeffreySu/WeiXinMPSDK · error · InvalidDataException
微信支付通知请求体超过允许上限 字节。
Error message
微信支付通知请求体超过允许上限 {maxBodyBytes} 字节。 What it means
Before buffering, ReadBodyAsync checks request.ContentLength; if the declared body length exceeds the configured maxBodyBytes limit it throws InvalidDataException with the limit in the message. This protects against oversized or malicious notification payloads.
Solutions
- Increase maxBodyBytes to a safe size (e.g. 1 MB) when constructing the handler
- Set maxBodyBytes to null if no limit is needed
- Compare against actual WeChat notification sizes before choosing the cap
- Check for duplicated/malformed requests behind a proxy that inflate Content-Length
Example fix
// before var handler = TenPayNotifyHandler.Create(httpContext, settings, maxBodyBytes: 512); // after var handler = TenPayNotifyHandler.Create(httpContext, settings, maxBodyBytes: 1024 * 1024);
Defensive patterns
Strategy: try-catch
Validate before calling
if (httpContext.Request.ContentLength > MaxNotificationBytes)
return BadRequest("通知体过大。"); Try / catch
try { var body = await ReadBodyAsync(httpContext, maxBodyBytes, ct); }
catch (InvalidDataException ex) when (ex.Message.Contains("超过允许上限"))
{ logger.LogWarning("通知体超限: {0}", ex.Message); return BadRequest(); } Prevention
- Size the limit above the largest legitimate WeChat notification (~a few KB)
- Reject oversized requests at the reverse proxy as first line of defense
When it happens
Trigger: A POST/PUT/PATCH notification whose Content-Length header exceeds maxBodyBytes passed to ReadBodyAsync/the notify handler.
Common situations: Very small maxBodyBytes configured (e.g. a few KB) while WeChat sends a normal notification (~1-2 KB JSON plus padding), or proxy/CDN inflating the body.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
- 请求体上限必须大于 0。
- 超时时间必须大于 0,或使用 Timeout.Infinite。
- 品牌 API 密钥不能为空。
- brandApiCredentials
- 没有设置证书加密类型(EncryptionType)
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/bc869bd1a1fa93cd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs:177
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);
}
else
{
request.EnableBuffering(bufferThreshold: 30 * 1024);
}
request.Body.Position = 0;
using (var linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
httpContext.RequestAborted))
using (var bodyBuffer = new MemoryStream(request.ContentLength.HasValue
? (int)Math.Min(request.ContentLength.Value, maxBodyBytes ?? int.MaxValue)
: 0))View on GitHub (pinned to be573f6f94)