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

  1. Increase maxBodyBytes to a safe size (e.g. 1 MB) when constructing the handler
  2. Set maxBodyBytes to null if no limit is needed
  3. Compare against actual WeChat notification sizes before choosing the cap
  4. 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

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


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)