JeffreySu/WeiXinMPSDK · error · ArgumentNullException
data 不能为 null!
Error message
data 不能为 null!
What it means
Inside TenPayApiRequest.GetHttpResponseMessageAsync, after validating timeOut and building the HttpRequestMessage, the method branches on the request method; a null data (request body) is rejected before the request is sent. The exception fires when a caller requests a POST/PUT-style API call without supplying the JSON object to serialize as the body — WeChat Pay V3 endpoints require a payload for these methods, so a null body is invalid.
Solutions
- Pass an empty object (new { }) or the actual request DTO for POST/PUT/PATCH calls
- Set checkDataNotNull: false if the endpoint genuinely accepts an empty body
- Check that your DTO construction/mapping does not return null
Example fix
// before
await tenPayApiRequest.PostAsync(url, null);
// after
await tenPayApiRequest.PostAsync(url, new { }); // or the required request DTO Defensive patterns
Strategy: validation
Validate before calling
if (data == null && checkDataNotNull) data = new { }; // or throw early with context Try / catch
try { await request.PostAsync(url, data); }
catch (ArgumentNullException ex) when (ex.ParamName?.StartsWith("data") == true) { log.Error("POST 请求 body 为空", ex); throw; } Prevention
- Always pass a request DTO (even empty new { }) for POST/PUT/PATCH
- Pass checkDataNotNull:false only for endpoints documented to accept empty bodies
- Map/serialize request models defensively so nulls don't propagate
When it happens
Trigger: Calling PostAsync/PutAsync/PatchAsync with data == null (e.g. an empty request DTO not constructed, or a LINQ/serialization result that returned null) while leaving checkDataNotNull at its default true.
Common situations: Sending POST endpoints that technically accept no body but calling the method without an object; a null model from a failed mapping step.
Related errors
- tenPayV3Info 参数不能为空!
- brandApiCredentials
- fileStream
- buttonGroupBase不可以为空!
- WeixinPayInfoCollection尚未注册Partner:
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/f1005d01a1366870.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:232
if (timeOut <= 0 && timeOut != Timeout.Infinite)
{
throw new ArgumentOutOfRangeException(nameof(timeOut), "超时时间必须大于 0,或使用 Timeout.Infinite。 ");
}
using var request = new HttpRequestMessage(GetHttpMethod(requestMethod), url);
switch (requestMethod)
{
case ApiRequestMethod.GET:
case ApiRequestMethod.DELETE:
WeixinTrace.Log(url);
break;
case ApiRequestMethod.POST:
case ApiRequestMethod.PUT:
case ApiRequestMethod.PATCH:
if (checkDataNotNull)
{
_ = data ?? throw new ArgumentNullException($"{nameof(data)} 不能为 null!");
}
string jsonString = data != null
? data.ToJson(false, RequestJsonSerializerSettings)
: "";
WeixinTrace.SendApiPostDataLog(url, jsonString);
request.Content = new StringContent(jsonString, Encoding.UTF8, mediaType: "application/json");
break;
default:
throw new ArgumentOutOfRangeException(nameof(requestMethod));
}
using var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
if (timeOut != Timeout.Infinite)
{
timeoutCancellationTokenSource.CancelAfter(timeOut);
}
View on GitHub (pinned to be573f6f94)