{"record":{"id":"ff3b118c6cbe2df4","repo":"JeffreySu/WeiXinMPSDK","slug":"finance-sdk-json","errorCode":null,"errorMessage":"无法解析 Finance SDK 返回的会话内容 JSON。","messagePattern":"无法解析 Finance SDK 返回的会话内容 JSON。","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/MsgAudit/MsgAuditFinanceClient.cs","lineNumber":108,"sourceCode":"                var slice = _nativeApi.NewSlice();\n                EnsureNativeHandle(slice, \"NewSlice\");\n                try\n                {\n                    var errorCode = _nativeApi.GetChatData(_sdk, sequence, limit, _proxy,\n                        _proxyPassword, _timeoutSeconds, slice);\n                    ThrowIfNativeError(errorCode, \"GetChatData\");\n\n                    var json = _nativeApi.GetSliceContent(slice);\n                    if (string.IsNullOrWhiteSpace(json))\n                    {\n                        throw new InvalidDataException(\"Finance SDK 返回的会话内容为空。\");\n                    }\n\n                    var result = JsonSerializer.Deserialize(json,\n                        MsgAuditFinanceJsonSerializerContext.Default.MsgAuditFinanceChatDataResult);\n                    if (result == null)\n                    {\n                        throw new InvalidDataException(\"无法解析 Finance SDK 返回的会话内容 JSON。\");\n                    }\n\n                    result.raw_json = json;\n                    return result;\n                }\n                finally\n                {\n                    _nativeApi.FreeSlice(slice);\n                }\n            }\n        }\n\n        /// <summary>\n        /// 使用官方 Finance SDK 解密一条会话消息。\n        /// </summary>\n        /// <param name=\"decryptedRandomKey\">\n        /// 对 <c>encrypt_random_key</c> 完成 Base64 解码并使用企业 RSA 私钥按 PKCS#1 解密后得到的随机密钥。\n        /// </param>","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/MsgAudit/MsgAuditFinanceClient.cs#L90-L126","documentation":"After a successful native call, GetChatData deserializes the slice JSON via System.Text.Json source-generated JsonSerializer. If deserialization returns null (possible when the JSON encodes a null value), the client throws InvalidDataException because it cannot distinguish a valid empty result from a malformed payload.","triggerScenarios":"The Finance SDK returns JSON that deserializes to null for MsgAuditFinanceChatDataResult — e.g. payload literally \"null\", or a serializer/source-generator context mismatch (type not registered, trimmed assembly).","commonSituations":"AOT/trimming dropping the MsgAuditFinanceChatDataResult metadata so Deserialize returns null; SDK returning unusual payloads; custom JsonSerializerOptions missing the source-gen context.","solutions":["Capture the raw JSON (result.raw_json is attached on success) — log json before parse to inspect the payload","Ensure MsgAuditFinanceJsonSerializerContext is used exactly as shipped and the type is registered (no trimming/AOT stripping)","Wrap the call in try/catch for InvalidDataException/JsonException and treat as retryable pull","Upgrade the SDK package if the SDK payload schema changed and the target type no longer matches"],"exampleFix":"// before\nvar result = client.GetChatData(seq, 1000);\nProcess(result.ChatData);\n// after\ntry { var result = client.GetChatData(seq, 1000); Process(result.ChatData); }\ncatch (InvalidDataException ex) { _logger.LogWarning(ex, \"Unparseable finance payload, retrying\"); ScheduleRetry(seq); }","handlingStrategy":"try-catch","validationCode":"// pre-check payload looks like an object\nif (json != null && json.Trim() == \"null\") throw new InvalidOperationException(\"Finance SDK returned null payload\");","typeGuard":null,"tryCatchPattern":"try { var data = client.GetChatData(seq, 1000); } catch (InvalidDataException ex) { log.Warn(ex, \"Unparseable finance payload\"); ScheduleRetry(seq); }","preventionTips":["Keep MsgAuditFinanceJsonSerializerContext intact under trimming/AOT (add DynamicallyAccessedMembers if needed)","Log raw_json on failure for upstream bug reports","Pin SDK package versions and retest after upgrades"],"tags":["json","deserialization","native-sdk","wecom"],"backgroundTag":"json-unmarshal-failed","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"}