JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException
requestMethod
Error message
requestMethod
What it means
ArgumentOutOfRangeException for the requestMethod parameter in GetHttpResponseMessageAsync's default switch branch: the supplied ApiRequestMethod value is not one of the handled GET/POST/PUT/PATCH cases.
Solutions
- Only pass valid ApiRequestMethod members (GET, POST, PUT, PATCH)
- If loading from config, validate/parse the value against the enum before use
- Align library versions so enum definitions match across projects
Example fix
// before var method = (ApiRequestMethod)someInt; await request.GetAsync(url, requestMethod: method); // after var method = Enum.IsDefined(typeof(ApiRequestMethod), someInt) ? (ApiRequestMethod)someInt : ApiRequestMethod.POST; await request.GetAsync(url, requestMethod: method);
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(ApiRequestMethod), requestMethod))
throw new InvalidDataException($"未知的 ApiRequestMethod: {requestMethod}"); Try / catch
try { await request.GetAsync(url, requestMethod: method); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "requestMethod") { log.Error("非法 requestMethod", ex); throw; } Prevention
- Only assign ApiRequestMethod via enum members, never unchecked int casts
- When persisting method to config/DB, parse with Enum.TryParse and reject unknown values
- Keep Senparc.Weixin.TenPay versions aligned across all projects
When it happens
Trigger: Casting an arbitrary int to ApiRequestMethod (e.g. (ApiRequestMethod)7) or passing an enum member added in a newer library version while running an older build of TenPayApiRequest.
Common situations: Persisting the enum value to config/database and reading back an out-of-range number; assembly version mismatch between projects in the solution.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- requestMethod
- WeixinPayInfoCollection尚未注册Partner:
- TenPayV3InfoCollection尚未注册Mch:
- 没有设置证书加密类型(EncryptionType)
- sub_orders 参数必须在 2 到 10 之间!
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/ed57c3f87095af47.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:242
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);
}
return await _client.Value.SendAsync(request, completionOption, timeoutCancellationTokenSource.Token).ConfigureAwait(false);
}
private static HttpMethod GetHttpMethod(ApiRequestMethod requestMethod)
{
switch (requestMethod)
{
case ApiRequestMethod.GET:
return HttpMethod.Get;
case ApiRequestMethod.POST:View on GitHub (pinned to be573f6f94)