{"record":{"id":"4e8a4fbef15a4fdb","repo":"JeffreySu/WeiXinMPSDK","slug":"filestream","errorCode":null,"errorMessage":"fileStream","messagePattern":"fileStream","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs","lineNumber":381,"sourceCode":"        {\n            return RequestMultipartCoreAsync(url, fileName, fileStream,\n                cancellationToken,\n                MultipartMetaFieldStyle.FilenameAndFileDigest, timeOut,\n                checkSign, createDefaultInstance, maxFileBytes);\n        }\n\n        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","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs#L363-L399","documentation":"RequestMultipartCoreAsync is the shared implementation behind the four public multipart upload methods (RequestMultipartAsync and its with-max-size / file-digest / filename-and-digest variants). It validates the fileStream argument and throws ArgumentNullException when the uploaded content stream is null, because a multipart file upload cannot be constructed without stream content to write into the multipart body. The offending input is the fileStream parameter passed by one of the public wrappers.","triggerScenarios":"Passing a null Stream — commonly File.OpenRead on a nonexistent path assigned before the call and swallowed, or a stream from an async open that returned null.","commonSituations":"File missing at runtime (wrong working directory), stream already disposed and nulled, or conditional file-open logic that skipped opening.","solutions":["Open the file stream before the call and assert it is non-null and readable (stream.CanRead)","Check the file exists with File.Exists before opening to give a better error","Ensure the stream is not disposed/closed before the async upload completes"],"exampleFix":"// before\nusing FileStream fs = fileExists ? File.OpenRead(path) : null;\nawait apiRequest.RequestMultipartAsync(url, fileName, fs);\n// after\nif (!File.Exists(path)) throw new FileNotFoundException(path);\nusing var fs = File.OpenRead(path);\nawait apiRequest.RequestMultipartAsync(url, fileName, fs);","handlingStrategy":"validation","validationCode":"if (fileStream == null || !fileStream.CanRead)\n    throw new InvalidOperationException(\"文件流不可读，请先打开文件\");","typeGuard":null,"tryCatchPattern":"try { await apiRequest.RequestMultipartAsync(url, fileName, fileStream); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"fileStream\") { log.Error(\"文件流为空\", ex); throw; }","preventionTips":["Check File.Exists before File.OpenRead; wrap open in try-catch for FileNotFoundException","Keep the stream open until the async upload completes (avoid premature using/dispose)","Accept Stream parameters in your own layer and fail fast on null"],"tags":["wechat-pay","multipart-upload","null-argument"],"backgroundTag":"null-argument","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"}