{"record":{"id":"ef3bd2b97fb9a98f","repo":"JeffreySu/WeiXinMPSDK","slug":"encryptiontype-tenpayhttpclient","errorCode":null,"errorMessage":"没有设置证书加密类型（EncryptionType）","messagePattern":"没有设置证书加密类型（EncryptionType）","errorType":"exception","errorClass":"WeixinException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs","lineNumber":132,"sourceCode":"        {\n            NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore\n        };\n        private readonly SenparcHttpClient _httpClient;\n        private ISenparcWeixinSettingForTenpayV3 _tenpayV3Setting;\n        private readonly HttpClient _client;\n        private readonly ISigner _signer;\n        private readonly IVerifier _verifier;\n        private CertType CertType => _tenpayV3Setting.EncryptionType.Value;\n\n        public TenPayHttpClient(SenparcHttpClient httpClient, ISenparcWeixinSettingForTenpayV3 senparcWeixinSettingForTenpayV3 = null)\n        {\n            this._httpClient = httpClient;\n            this._client = this._httpClient.Client;\n            _tenpayV3Setting = senparcWeixinSettingForTenpayV3 ?? Senparc.Weixin.Config.SenparcWeixinSetting.TenpayV3Setting;\n\n            if (!_tenpayV3Setting.EncryptionType.HasValue)\n            {\n                throw new Senparc.Weixin.Exceptions.WeixinException(\"没有设置证书加密类型（EncryptionType）\");\n            }\n\n            //从工厂获得签名和验签的方法类\n            _signer = TenPayCertFactory.GetSigner(CertType);\n            _verifier = TenPayCertFactory.GetVerifier(CertType);\n\n        }\n\n        public Task<T> SendAsync<T>(string url, object data, int timeOut = Senparc.Weixin.Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func<T> createDefaultInstance = null)\n                   where T : ReturnJsonBase/*, new()*/\n        {\n            return SendAsync(url, data, CancellationToken.None, timeOut, requestMethod, checkSign, createDefaultInstance);\n        }\n\n        public async Task<T> SendAsync<T>(string url, object data, CancellationToken cancellationToken, int timeOut = Senparc.Weixin.Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func<T> createDefaultInstance = null)\n                   where T : ReturnJsonBase/*, new()*/\n        {\n            T result = null;","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/TenPayHttpClient/TenPayHttpClient.cs#L114-L150","documentation":"TenPayHttpClient's constructor requires the TenpayV3Setting to specify EncryptionType (RSA or SM certificate scheme), because it uses that value to select the signer/verifier via TenPayCertFactory. If SenparcWeixinSetting.TenpayV3Setting has no EncryptionType set, the client cannot be constructed and a WeixinException is thrown immediately.","triggerScenarios":"Creating TenPayHttpClient (directly or via GetInstance) while the bound SenparcWeixinSetting has EncryptionType == null — e.g. the TenpayV3Setting section was not populated with the EncryptionType key.","commonSituations":"Upgrading from older Senparc.Weixin versions that predate the EncryptionType setting; copying appsettings from an older sample; configuring TenPayV3 via code but forgetting to assign EncryptionType; running under the SM (国密) pilot without setting the enum.","solutions":["Set EncryptionType on your TenpayV3Setting (RSA for standard certificates, SM for 国密) in appsettings.json or in code before constructing the client.","If configuring via SenparcWeixinSetting, verify the bound section actually contains the EncryptionType field and that binding succeeds.","After upgrading the package, migrate your config to include the newly required EncryptionType key."],"exampleFix":"// before (appsettings.json)\n\"TenpayV3Setting\": {\n  \"TenPayV3_MchId\": \"1900000001\",\n  \"TenPayV3_APIv3Key\": \"...\"\n}\n// after\n\"TenpayV3Setting\": {\n  \"TenPayV3_MchId\": \"1900000001\",\n  \"TenPayV3_APIv3Key\": \"...\",\n  \"EncryptionType\": \"RSA\"\n}","handlingStrategy":"validation","validationCode":"if (!setting.TenpayV3Setting.EncryptionType.HasValue)\n    throw new InvalidOperationException(\"Configure TenpayV3Setting.EncryptionType (RSA|SM) before creating TenPayHttpClient.\");","typeGuard":"bool HasEncryptionType(SenparcWeixinSetting s) => s?.TenpayV3Setting?.EncryptionType is CertType t and (CertType.RSA or CertType.SM);","tryCatchPattern":"try { var client = new TenPayHttpClient(httpClientFactory, setting); }\ncatch (WeixinException ex) { logger.LogCritical(ex, \"TenPayV3 misconfigured\"); throw; }","preventionTips":["Always include EncryptionType in appsettings TenpayV3Setting after package upgrades.","Fail fast at startup: construct the client once at boot, not lazily on first request.","Validate config binding with a startup health check."],"tags":["wechat-pay","config","encryption","initialization"],"backgroundTag":"missing-required-config-field","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}