{"record":{"id":"28b1138cb2b347fd","repo":"JeffreySu/WeiXinMPSDK","slug":"0-tenpaynotifyhandler","errorCode":null,"errorMessage":"请求体上限必须大于 0。","messagePattern":"请求体上限必须大于 0。","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs","lineNumber":166,"sourceCode":"        public static async Task<TenPayNotifyHandler> CreateAsync(\n            HttpContext httpContext,\n            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null,\n            int maxBodyBytes = DefaultMaxBodyBytes,\n            CancellationToken cancellationToken = default)\n        {\n            var notificationBody = await ReadBodyAsync(httpContext, maxBodyBytes, cancellationToken).ConfigureAwait(false);\n            return new TenPayNotifyHandler(httpContext, senparcWeixinSettingForTenpayV3, notificationBody);\n        }\n\n        private static async Task<NotificationBody> ReadBodyAsync(\n            HttpContext httpContext,\n            int? maxBodyBytes,\n            CancellationToken cancellationToken)\n        {\n            _ = httpContext ?? throw new ArgumentNullException(nameof(httpContext));\n            if (maxBodyBytes.HasValue && maxBodyBytes.Value <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(maxBodyBytes), \"请求体上限必须大于 0。\");\n            }\n\n            var request = httpContext.Request;\n            if (request.Method != \"POST\" && request.Method != \"PUT\" && request.Method != \"PATCH\")\n            {\n                return NotificationBody.Empty;\n            }\n\n            if (maxBodyBytes.HasValue && request.ContentLength > maxBodyBytes.Value)\n            {\n                throw new InvalidDataException($\"微信支付通知请求体超过允许上限 {maxBodyBytes} 字节。\");\n            }\n\n            if (maxBodyBytes.HasValue)\n            {\n                request.EnableBuffering(bufferThreshold: 30 * 1024, bufferLimit: maxBodyBytes.Value);\n            }\n            else","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs#L148-L184","documentation":"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。\").","triggerScenarios":"Calling ReadBodyAsync / the notify handler with maxBodyBytes set to 0 or a negative integer.","commonSituations":"Misreading the parameter as \"unlimited\" (passing 0), computing the limit from config that defaulted to 0, or off-by-one arithmetic.","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\""],"exampleFix":"// before\nvar body = await ReadBodyAsync(httpContext, 0, ct);\n// after\nvar body = await ReadBodyAsync(httpContext, 1024 * 1024, ct); // or null for no limit","handlingStrategy":"validation","validationCode":"if (maxBodyBytes is <= 0) throw new ArgumentException(\"maxBodyBytes 必须为正数或 null。\", nameof(maxBodyBytes));","typeGuard":"bool IsValidBodyLimit(int? n) => n is null || n > 0;","tryCatchPattern":"try { var body = await ReadBodyAsync(httpContext, maxBodyBytes, ct); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"maxBodyBytes\") { logger.LogError(ex, \"maxBodyBytes 配置非法\"); }","preventionTips":["Centralize the body-size constant instead of hard-coding per call","Validate limits from config at startup","Use null, not 0, for \"no limit\""],"tags":["argument-out-of-range","validation","tenpay"],"backgroundTag":"argument-out-of-range","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}