JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException (certType: RSA/SM only)

Error message

ArgumentOutOfRangeException (certType: RSA/SM only)

What it means

TenPaySignerFactory.GetVerifier mirrors GetSigner for verification objects: CertType.RSA yields SHA256WithRSAVerifier, CertType.SM yields SM3WithSM2Verifier, and any other value throws ArgumentOutOfRangeException(nameof(certType)).

Solutions

  1. Set EncryptionType to RSA or SM in TenpayV3Setting.
  2. Use Enum.IsDefined to validate config-derived CertType values.
  3. Upgrade the package if a new certificate scheme is required.

Example fix

// before
services.Configure(o => o.TenpayV3Setting.EncryptionType = (CertType)7);
// after
services.Configure(o => o.TenpayV3Setting.EncryptionType = CertType.SM);
Defensive patterns

Strategy: validation

Validate before calling

if (certType is not (CertType.RSA or CertType.SM))
    throw new InvalidOperationException("Verifier supports only CertType.RSA or CertType.SM.");

Type guard

bool IsSupportedVerifierType(CertType t) => t is CertType.RSA or CertType.SM;

Try / catch

try { var verifier = TenPaySignerFactory.GetVerifier(certType); }
catch (ArgumentOutOfRangeException) { logger.LogError("Unsupported certType for verifier: {CertType}", certType); }

Prevention

When it happens

Trigger: TenPayHttpClient construction with an unsupported CertType/EncryptionType, since GetVerifier is invoked alongside GetSigner in the constructor; or direct calls to GetVerifier with an invalid enum value.

Common situations: Same family as the signer error: bad enum from config, int casts, or library versions lacking support for a newer certificate scheme.

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


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/c5cf764821ef6227. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPaySignerFactory.cs:48

            }
        }

        /// <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:
                    throw new ArgumentOutOfRangeException(nameof(certType));
            }
        }
    }
}

View on GitHub (pinned to be573f6f94)