JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException
certType
Error message
certType
What it means
TenPaySignerFactory.GetSigner only supports CertType.RSA (SHA256WithRSASigner) and CertType.SM (SM3WithSM2Signer); any other CertType value reaches the default arm and throws ArgumentOutOfRangeException(nameof(certType)).
Solutions
- Set EncryptionType to CertType.RSA (standard) or CertType.SM (国密) in your TenpayV3Setting.
- Validate any config-sourced CertType with Enum.IsDefined before use.
- If you need an unsupported scheme, upgrade the Senparc.Weixin.TenPay package to a version that supports it.
Example fix
// before var certType = (CertType)0; // undefined // after var certType = CertType.RSA; // or CertType.SM
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(CertType), certType) || certType is not (CertType.RSA or CertType.SM))
throw new InvalidOperationException("EncryptionType must be RSA or SM."); Type guard
bool IsSupportedCertType(CertType t) => t is CertType.RSA or CertType.SM;
Try / catch
try { var signer = TenPaySignerFactory.GetSigner(certType); }
catch (ArgumentOutOfRangeException) { logger.LogError("Unsupported certType {CertType}", certType); } Prevention
- Configure EncryptionType only with named enum members.
- Validate enum config values at startup with Enum.IsDefined.
- Keep the Senparc.Weixin.TenPay package updated if you use 国密.
When it happens
Trigger: Constructing TenPayHttpClient (which calls TenPayCertFactory/GetSigner) with an EncryptionType/CertType that is not RSA or SM — typically an invalid enum cast or an unbound default 0 value if the enum doesn't define it.
Common situations: Config value parsed into an undefined enum member; int cast to CertType; new CertType member added by a newer WeChat Pay scheme but the installed Senparc library version doesn't support it.
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
- ArgumentOutOfRangeException (certType: RSA/SM only)
- sendType
- 没有设置证书加密类型(EncryptionType)
- 超时时间必须大于 0,或使用 Timeout.Infinite。
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/0803374dfd29911f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPaySignerFactory.cs:29
/// </summary>
public static class TenPayCertFactory
{
/// <summary>
/// 获取签名对象
/// </summary>
/// <param name="certType"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static ISigner GetSigner(CertType certType)
{
switch (certType)
{
case CertType.RSA:
return new SHA256WithRSASigner();
case CertType.SM:
return new SM3WithSM2Signer();
default:
throw new ArgumentOutOfRangeException(nameof(certType));
}
}
/// <summary>
/// 获取验签对象
/// </summary>
/// <param name="certType"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static IVerifier GetVerifier(CertType certType)
{
switch (certType)
{
case CertType.RSA:
return new SHA256WithRSAVerifier();
case CertType.SM:
return new SM3WithSM2Verifier();
default:View on GitHub (pinned to be573f6f94)