JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException
requestMethod
Error message
requestMethod
What it means
GetHttpMethod maps the ApiRequestMethod enum to System.Net.Http.HttpMethod; any value outside GET/POST/PUT/PATCH/DELETE falls into the default arm and throws ArgumentOutOfRangeException(nameof(requestMethod)). The message is just the parameter name.
Solutions
- Use only defined ApiRequestMethod members: GET, POST, PUT, PATCH, DELETE.
- Validate/parse inbound values with Enum.TryParse<ApiRequestMethod> before passing them on.
- If you need a verb the library doesn't map, extend ApiRequestMethod and the switch, or use a lower-level HttpClient path.
Example fix
// before
var method = (ApiRequestMethod)99; // invalid
// after
if (!Enum.TryParse<ApiRequestMethod>(raw, out var method))
method = ApiRequestMethod.GET; Defensive patterns
Strategy: type-guard
Validate before calling
if (!Enum.IsDefined(typeof(ApiRequestMethod), method))
throw new ArgumentException($"Unsupported ApiRequestMethod: {method}"); Type guard
bool IsValidMethod(ApiRequestMethod m) => m is ApiRequestMethod.GET or ApiRequestMethod.POST or ApiRequestMethod.PUT or ApiRequestMethod.PATCH or ApiRequestMethod.DELETE;
Try / catch
try { await client.SendAsync(method, url); }
catch (ArgumentOutOfRangeException ex) { logger.LogError(ex, "Unknown HTTP method enum value"); } Prevention
- Only assign ApiRequestMethod from named enum members, never raw ints.
- Parse external input with Enum.TryParse and reject unknown values early.
- Add unit tests covering every verb your app uses.
When it happens
Trigger: Passing an undefined/invalid ApiRequestMethod value (e.g. an int cast that doesn't correspond to a defined enum member) into SendAsync/request building, typically via GetHttpMethod.
Common situations: Enum deserialized from config or a database string that produced a non-member value; casting an int constant to ApiRequestMethod; a custom extension that introduced a new verb the switch doesn't handle.
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
- certType
- ArgumentOutOfRangeException (certType: RSA/SM only)
- sendType
- requestMethod
- 超时时间必须大于 0,或使用 Timeout.Infinite。
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/835f9f71cdcc3865.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs:248
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
var userAgentValues = UserAgentValues.Instance;
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Senparc.Weixin.TenPayV3-C#", userAgentValues.TenPayV3Version));
request.Headers.UserAgent.Add(new ProductInfoHeaderValue($"(Senparc.Weixin {userAgentValues.SenparcWeixinVersion})"));
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(".NET", userAgentValues.RuntimeVersion));
request.Headers.UserAgent.Add(new ProductInfoHeaderValue($"({userAgentValues.OSVersion})"));
}
protected HttpMethod GetHttpMethod(ApiRequestMethod requestMethod)
{
return requestMethod switch
{
ApiRequestMethod.GET => HttpMethod.Get,
ApiRequestMethod.POST => HttpMethod.Post,
ApiRequestMethod.PUT => HttpMethod.Put,
ApiRequestMethod.PATCH => HttpMethod.Patch,
ApiRequestMethod.DELETE => HttpMethod.Delete,
_ => throw new ArgumentOutOfRangeException(nameof(requestMethod)),
};
}
protected async Task<string> GenerateAuthorizationHeader(HttpRequestMessage request)
{
string method = request.Method.ToString();
string body = "";
if (method == "POST" || method == "PUT" || method == "PATCH")
{
var content = request.Content;
body = await content.ReadAsStringAsync();
}
string uri = request.RequestUri.PathAndQuery;
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = Path.GetRandomFileName();
string message = $"{method}\n{uri}\n{timestamp}\n{nonce}\n{body}\n";View on GitHub (pinned to be573f6f94)