JeffreySu/WeiXinMPSDK · error · WeixinException

没有设置证书加密类型(EncryptionType)

Error message

没有设置证书加密类型(EncryptionType)

What it means

TenPayHttpHandler's constructor requires the TenpayV3 setting to declare which certificate encryption type (EncryptionType) is used, because it determines how the merchant certificate is encrypted/processed for API v3 requests. If EncryptionType is null, the handler cannot proceed and throws WeixinException.

Solutions

  1. Set EncryptionType on the TenpayV3Setting section (e.g. "EncryptionType": "MD5" or "HMAC-SHA256" per the SDK enum) in configuration
  2. Explicitly pass a fully populated ISenparcWeixinSettingForTenpayV3 to the TenPayHttpHandler constructor
  3. Register TenpayV3Setting via Config.SenparcWeixinSetting before constructing the handler
  4. Check SenparcWeixinSetting.TenpayV3Setting.EncryptionType.HasValue before instantiating the handler

Example fix

// before
var handler = new TenPayHttpHandler();
// after
Config.SenparcWeixinSetting.TenpayV3Setting.EncryptionType = EncryptionType.MD5;
var handler = new TenPayHttpHandler();
Defensive patterns

Strategy: validation

Validate before calling

var setting = Config.SenparcWeixinSetting.TenpayV3Setting;
if (setting is null || !setting.EncryptionType.HasValue)
    throw new InvalidOperationException("TenpayV3Setting.EncryptionType 未配置。");

Type guard

bool IsTenpayV3SettingValid(ISenparcWeixinSettingForTenpayV3 s) => s != null && s.EncryptionType.HasValue;

Prevention

When it happens

Trigger: Constructing TenPayHttpHandler with a SenparcWeixinSettingForTenpayV3 whose EncryptionType property is not set, or relying on the global Config.SenparcWeixinSetting.TenpayV3Setting which lacks EncryptionType.

Common situations: Developers migrating from TenPay V2 config blocks, copying old sample configs that predate the EncryptionType field, or forgetting to configure appsettings for TenpayV3Setting.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayHttpHandler.cs:105

        }

        private TenPayHttpHandler(
            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3,
            TenPayBrandApiCredentials brandApiCredentials)
        {
            InnerHandler = new HttpClientHandler();

            _brandApiCredentials = brandApiCredentials;
            if (_brandApiCredentials != null)
            {
                return;
            }

            _tenpayV3Setting = senparcWeixinSettingForTenpayV3 ?? Senparc.Weixin.Config.SenparcWeixinSetting.TenpayV3Setting;

            if (!_tenpayV3Setting.EncryptionType.HasValue)
            {
                throw new Senparc.Weixin.Exceptions.WeixinException("没有设置证书加密类型(EncryptionType)");
            }
        }

        /// <summary>
        /// 重写 SendAsync 方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var auth = await BuildAuthAsync(request);

            var authorizationValue = _brandApiCredentials != null
                ? TenPayBrandApiCredentials.AuthorizationType
                : _tenpayV3Setting.EncryptionType == CertType.SM

View on GitHub (pinned to be573f6f94)