JeffreySu/WeiXinMPSDK · error · ArgumentNullException
appKey
Error message
appKey
What it means
XPayApi.GeneratePaySign (WeChat Mini Game virtual payment) throws ArgumentNullException when the appKey parameter is null, empty, or whitespace. The appKey is required as the HMAC signing key for the pay-sign computation.
Solutions
- Supply a non-empty appKey from your configuration (微信小程序虚拟支付 Key)
- Add a startup config check that fails fast if the XPay appKey is missing
- Verify the config source actually populated the value (env var name, config section)
Example fix
// before var sign = XPayApi.GeneratePaySign(null, uri, data); // after var sign = XPayApi.GeneratePaySign(_options.XPayAppKey, uri, data); // _options.XPayAppKey validated non-empty at startup
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(appKey)) throw new InvalidOperationException("XPay appKey is not configured"); Try / catch
try { return XPayApi.GeneratePaySign(appKey, uri, data); }
catch (ArgumentNullException ex) { logger.LogError(ex, "XPay signing input missing"); throw new ConfigurationErrorsException("XPay appKey/uri/data invalid", ex); } Prevention
- Validate XPay configuration at application startup
- Store the appKey in a validated options class with [Required] attributes
- Use IOptions validation so blank keys fail at boot, not at request time
When it happens
Trigger: Calling XPayApi.GeneratePaySign(data, uri) with an appKey string that is null/empty — e.g. the key was never loaded from configuration or the config value is blank.
Common situations: appsettings/config file missing the XPay appKey entry; environment variable not set in the deployment environment; developer passes the wrong variable (appId instead of appKey).
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/52e0ec746a5b49ea.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.WxOpen/src/Senparc.Weixin.WxOpen/Senparc.Weixin.WxOpen/AdvancedAPIs/XPay/XPayApi.cs:1281
}, accessTokenOrAppId);
}
#endregion
#region 扩展方法
/// <summary>
/// 生成pay_sig
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="appKey"></param>
/// <param name="uri">例:/xpay/query_user_balance</param>
/// <param name="data"></param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException"></exception>
public static string GeneratePaySign<T>(string appKey, string uri, T data)
{
if (string.IsNullOrWhiteSpace(appKey))
{
throw new ArgumentNullException("appKey");
}
if (string.IsNullOrWhiteSpace(uri))
{
throw new ArgumentNullException("uri");
}
if (data == null)
{
throw new ArgumentNullException("data");
}
var signData = JsonConvert.SerializeObject(data);
var rawData = $"{uri}&{signData}";
var keyBytes = Encoding.UTF8.GetBytes(appKey);
var dataBytes = Encoding.UTF8.GetBytes(rawData);
using (var hmac = new HMACSHA256(keyBytes))
{
var hashBytes = hmac.ComputeHash(dataBytes);View on GitHub (pinned to be573f6f94)