{"record":{"id":"31d0e4cc2dc94df6","repo":"JeffreySu/WeiXinMPSDK","slug":"0-tenpayapirequest","errorCode":null,"errorMessage":"文件大小上限必须大于 0。","messagePattern":"文件大小上限必须大于 0。","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs","lineNumber":388,"sourceCode":"        private async Task<T> RequestMultipartCoreAsync<T>(string url, string fileName,\n            Stream fileStream, CancellationToken cancellationToken,\n            MultipartMetaFieldStyle metaFieldStyle, int timeOut, bool checkSign,\n            Func<T> createDefaultInstance, int? maxFileBytes = null)\n            where T : ReturnJsonBase, new()\n        {\n            if (string.IsNullOrWhiteSpace(fileName))\n            {\n                throw new ArgumentException(\"文件名不能为空。\", nameof(fileName));\n            }\n\n            _ = fileStream ?? throw new ArgumentNullException(nameof(fileStream));\n            if (timeOut <= 0 && timeOut != Timeout.Infinite)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeOut), \"超时时间必须大于 0，或使用 Timeout.Infinite。 \");\n            }\n            if (maxFileBytes.HasValue && maxFileBytes.Value <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(maxFileBytes),\n                    \"文件大小上限必须大于 0。\");\n            }\n            if (maxFileBytes.HasValue && fileStream.CanSeek &&\n                fileStream.Length - fileStream.Position > maxFileBytes.Value)\n            {\n                throw new InvalidDataException(\n                    $\"上传文件超过允许上限 {maxFileBytes.Value} 字节。\");\n            }\n\n            T result = null;\n            try\n            {\n                byte[] fileBytes;\n                int fileLength;\n                using (var memoryStream = new MemoryStream())\n                {\n                    var buffer = ArrayPool<byte>.Shared.Rent(81920);\n                    try","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs#L370-L406","documentation":"RequestMultipartCoreAsync validates that when maxFileBytes is supplied it must be strictly positive. If a caller passes 0 or a negative value, the library throws ArgumentOutOfRangeException because a non-positive upload limit is meaningless. This is a caller-side parameter contract error, not a server or network problem.","triggerScenarios":"Calling RequestMultipartAsync / RequestMultipartWithMaxSizeAsync / RequestMultipartWithFileDigestAsync / RequestMultipartWithFilenameAndFileDigestAsync with maxFileBytes = 0, negative, or a default-constructed value (e.g. int variable never assigned, or computed size limit that evaluated to <= 0).","commonSituations":"Passing 0 to mean 'no limit' (the library uses null for unlimited, not 0); computing the limit from configuration that resolved to 0; passing a negative due to a subtraction bug when deriving the cap.","solutions":["Pass null (omit) for maxFileBytes when no limit is desired — 0 is not the 'unlimited' sentinel.","Check the configured/upload limit value before calling and ensure it is > 0.","Fix the calculation that produced the non-positive limit (e.g. guard against zero-size configs).","Wrap the call in try/catch for ArgumentOutOfRangeException during development to surface the bad parameter early."],"exampleFix":"// before\nawait apiRequest.RequestMultipartWithMaxSizeAsync(url, jsonContent, fileStream, maxFileBytes: 0);\n// after\nawait apiRequest.RequestMultipartWithMaxSizeAsync(url, jsonContent, fileStream, maxFileBytes: 5 * 1024 * 1024); // or null for unlimited","handlingStrategy":"validation","validationCode":"if (maxFileBytes.HasValue && maxFileBytes.Value <= 0)\n    throw new ArgumentException(\"maxFileBytes must be positive or null for unlimited.\", nameof(maxFileBytes));","typeGuard":"bool IsValidMaxSize(long? maxFileBytes) => !maxFileBytes.HasValue || maxFileBytes.Value > 0;","tryCatchPattern":null,"preventionTips":["Use null, not 0, to mean 'no limit'.","Validate computed limits before calling the API.","Document the limit semantics in your configuration keys."],"tags":["argument-out-of-range","validation","weixin-pay"],"backgroundTag":"value-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"}