JeffreySu/WeiXinMPSDK · error · PlatformNotSupportedException
当前目标框架不提供 AES-GCM。请在 netstandard2.1、netcoreapp3.1…
Error message
当前目标框架不提供 AES-GCM。请在 netstandard2.1、netcoreapp3.1 或更新目标中解密通知资源。
What it means
DecryptResource is used to decrypt the encrypted `resource` node of a WeChat Work mini-program payment notification using AES-GCM. On .NET Framework 4.6.2 and netstandard2.0 targets the platform provides no AesGcm implementation, so the library deliberately throws PlatformNotSupportedException instead of attempting decryption. The full encrypted notification model can still be received; only decryption must happen on a newer target.
Solutions
- Retarget the consuming project to netstandard2.1, netcoreapp3.1, or a newer framework (net5/6/7+) where AesGcm is available
- Keep the net462/netstandard2.0 build and decrypt the notification in a separate service running a modern runtime
- Use a third-party AES-GCM implementation (e.g. BouncyCastle) and decrypt the ciphertext manually from resource.ciphertext instead of calling DecryptResource
Example fix
// before (csproj) <TargetFrameworks>net462;netstandard2.0</TargetFrameworks> // after <TargetFrameworks>net462;netstandard2.0;net6.0</TargetFrameworks>
Defensive patterns
Strategy: validation
Validate before calling
#if NET462 || NETSTANDARD2_0 #error DecryptResource requires netstandard2.1 or newer target #endif
Type guard
static bool SupportsAesGcm =>
!typeof(object).Assembly.GetName().Name.StartsWith("mono") &&
Environment.Version >= new Version(3, 0); Try / catch
try { var plain = msg.DecryptResource(aesKey); }
catch (PlatformNotSupportedException ex) { log.Warn("AES-GCM unsupported on this TFM"); return FallbackDecrypt(msg, aesKey); } Prevention
- Pin the deployment runtime to netcoreapp3.1+/net5+ when handling encrypted payment notifications
- Add a multi-target compile check so net462/netstandard2.0 builds cannot reference the decrypt path
- Document per-TFM capabilities where the notification handler is registered
When it happens
Trigger: Calling DecryptResource(encodingAesKey) on a RequestMessageEvent_MiniProgramPay when the consuming project targets net462 or netstandard2.0.
Common situations: Projects compiled against the .NET Framework or netstandard2.0 multi-target of the library trying to decrypt payment callbacks after upgrading the SDK; CI builds on an older TFM silently hitting the conditional-compilation branch.
Understand the failure class
Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.
Related errors
- Invalid hex length for uncompressed EC public key
- 证书中未包含 RSA 公钥。
- 证书中未包含 RSA 公钥。
- EncodingAESKey 必须为 43 个字符。
- 通知中不包含 resource 节点。
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/b5f43c106a1b4496.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_MiniProgramPay.cs:67
/// <summary>资源类型。</summary>
public string resource_type { get; set; }
/// <summary>加密业务资源。</summary>
public MiniProgramPayNotificationResource resource { get; set; }
/// <summary>通知摘要。</summary>
public string summary { get; set; }
/// <summary>
/// 使用“对外收款”应用回调配置中的 EncodingAESKey 解密业务资源。
/// .NET Framework 与 netstandard2.0 不提供平台 AES-GCM 实现,将抛出
/// <see cref="PlatformNotSupportedException"/>;这些目标仍可接收完整加密通知模型。
/// </summary>
public string DecryptResource(string encodingAesKey)
{
#if NET462 || NETSTANDARD2_0
throw new PlatformNotSupportedException(
"当前目标框架不提供 AES-GCM。请在 netstandard2.1、netcoreapp3.1 或更新目标中解密通知资源。");
#else
if (string.IsNullOrEmpty(encodingAesKey) || encodingAesKey.Length != 43)
{
throw new ArgumentException("EncodingAESKey 必须为 43 个字符。", nameof(encodingAesKey));
}
if (resource == null)
{
throw new InvalidOperationException("通知中不包含 resource 节点。");
}
if (!string.Equals(resource.algorithm, "AEAD_AES_256_GCM", StringComparison.OrdinalIgnoreCase))
{
throw new NotSupportedException($"不支持的通知资源加密算法:{resource.algorithm}");
}
var key = Convert.FromBase64String(encodingAesKey + "=");View on GitHub (pinned to be573f6f94)