JeffreySu/WeiXinMPSDK · error · WeixinException

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

Error message

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

What it means

Like the HttpHandler, TenPayNotifyHandler requires the TenpayV3 EncryptionType to know how to decrypt the notification resource; a null value throws WeixinException "没有设置证书加密类型(EncryptionType)" during handler construction.

Solutions

  1. Set EncryptionType in the TenpayV3Setting configuration section before handling callbacks
  2. Pass a complete ISenparcWeixinSettingForTenpayV3 (with EncryptionType) to the notify handler factory
  3. Verify Config.SenparcWeixinSetting.TenpayV3Setting is populated at startup (fail fast in Program.cs)
  4. Log the loaded setting object at startup to confirm EncryptionType binds correctly

Example fix

// before
"TenpayV3Setting": { "AppId": "wx123", "MerchantId": "mch1" }
// after
"TenpayV3Setting": { "AppId": "wx123", "MerchantId": "mch1", "EncryptionType": "MD5" }
Defensive patterns

Strategy: validation

Validate before calling

if (!Config.SenparcWeixinSetting.TenpayV3Setting.EncryptionType.HasValue)
    throw new InvalidOperationException("请先在 TenpayV3Setting 中设置 EncryptionType。");

Type guard

bool NotifySettingReady(ISenparcWeixinSettingForTenpayV3 s) => s?.EncryptionType.HasValue == true;

Try / catch

try { var handler = TenPayNotifyHandler.Create(httpContext, settings, body); ... }
catch (WeixinException ex) { logger.LogError(ex, "TenpayV3 配置不完整"); return StatusCode(500); }

Prevention

When it happens

Trigger: Creating a TenPayNotifyHandler (via its factory taking httpContext, settings and NotificationBody) while the effective TenpayV3Setting has no EncryptionType set.

Common situations: Same as error 150: missing or legacy appsettings TenpayV3Setting block, incomplete SenparcWeixinSetting registration in a notify callback controller.

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/9370bd268b7ca8df. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayNotifyHandler.cs:138

            : this(
                httpContext,
                senparcWeixinSettingForTenpayV3,
                ReadBodyAsync(httpContext, maxBodyBytes, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult())
        {
        }

        private TenPayNotifyHandler(
            HttpContext httpContext,
            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3,
            NotificationBody notificationBody)
        {
            _ = httpContext ?? throw new ArgumentNullException(nameof(httpContext));
            _httpContext = httpContext;
            _tenpayV3Setting = senparcWeixinSettingForTenpayV3 ?? Senparc.Weixin.Config.SenparcWeixinSetting.TenpayV3Setting;

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

            Body = notificationBody.Body;
            NotifyRequest = notificationBody.NotifyRequest;
        }

        /// <summary>
        /// 异步创建通知处理器,支持请求取消并限制请求体大小。
        /// </summary>
        public static async Task<TenPayNotifyHandler> CreateAsync(
            HttpContext httpContext,
            ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null,
            int maxBodyBytes = DefaultMaxBodyBytes,
            CancellationToken cancellationToken = default)
        {
            var notificationBody = await ReadBodyAsync(httpContext, maxBodyBytes, cancellationToken).ConfigureAwait(false);
            return new TenPayNotifyHandler(httpContext, senparcWeixinSettingForTenpayV3, notificationBody);
        }

View on GitHub (pinned to be573f6f94)