JeffreySu/WeiXinMPSDK · error · ArgumentException
AppId 不能为空。
Error message
AppId 不能为空。
What it means
The WxOpenApiClient constructor validates that appId is non-empty and throws ArgumentException ('AppId 不能为空。') otherwise. Every request executed through this client depends on a valid AppId, so the library refuses to construct an invalid instance.
Solutions
- Pass a valid Mini Program AppId (from the WeChat MP console) to the constructor
- Validate configuration at startup (fail fast if the AppId binding is empty)
- Check the config key/env var name matches what your loader reads
Example fix
// before
var client = new WxOpenApiClient(config["WxOpen:AppId"]); // may be null
// after
var appId = config["WxOpen:AppId"] ?? throw new InvalidOperationException("WxOpen:AppId not configured");
var client = new WxOpenApiClient(appId); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(appId)) throw new InvalidOperationException("WxOpen AppId is not configured"); Type guard
bool IsValidAppId(string appId) => !string.IsNullOrWhiteSpace(appId);
Try / catch
try { var client = new WxOpenApiClient(appId); }
catch (ArgumentException ex) { logger.LogCritical(ex, "AppId missing — check configuration"); throw; } Prevention
- Bind and validate Mini Program settings at startup (IOptions validation)
- Keep AppId/Secret in one settings section consumed everywhere
- Add a health check that constructs the client at boot
When it happens
Trigger: new WxOpenApiClient(null), new WxOpenApiClient(""), or a config-bound appId string that is null/whitespace at DI/composition time.
Common situations: appsettings section for the Mini Program not populated; environment variable not set in production; DI config binding silently leaving the string null.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/707c98b38ebe70e2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.WxOpen/src/Senparc.Weixin.WxOpen/Senparc.Weixin.WxOpen/CommonAPIs/WxOpenApiClient.cs:36
using Senparc.Weixin.CommonAPIs.ApiHandlerWapper;
using Senparc.Weixin.Entities;
using Senparc.Weixin.WxOpen.Containers;
namespace Senparc.Weixin.WxOpen
{
/// <summary>
/// 微信小程序实例 API 客户端。适合由业务 DI 容器按应用创建。
/// </summary>
public sealed class WxOpenApiClient
{
private static readonly int[] InvalidCredentialValues =
{ (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效 };
public WxOpenApiClient(string appId)
{
AppId = !string.IsNullOrWhiteSpace(appId)
? appId
: throw new ArgumentException("AppId 不能为空。", nameof(appId));
}
public string AppId { get; }
public T Execute<T>(Func<string, T> operation, bool retryInvalidCredential = true)
where T : WxJsonResult, new()
{
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}
return ApiHandlerWapperBase.TryCommonApiBase(
PlatformType.WxOpen,
() => AppId,
AccessTokenContainer.CheckRegistered,
AccessTokenContainer.GetAccessTokenResult,
InvalidCredentialValues,View on GitHub (pinned to be573f6f94)