JeffreySu/WeiXinMPSDK · error · ArgumentException
AppId 不能为空。
Error message
AppId 不能为空。
What it means
MpApiClient's constructor validates that the appId argument is a non-empty, non-whitespace string and throws ArgumentException('AppId 不能为空。') otherwise. The library treats a missing AppId as unrecoverable developer error, so it fails fast at construction time rather than when the first API call executes.
Solutions
- Pass the actual WeChat MP AppId string to the MpApiClient constructor.
- Check where the appId value originates (config file, env var, database) and confirm it is populated before constructing the client.
- If the value comes from configuration, add a startup validation that fails fast with a clear message when the AppId setting is missing or blank.
- Trim user-supplied/config values only after verifying they are non-whitespace, never before checking emptiness.
Example fix
// before
var client = new MpApiClient(configuration["Weixin:AppId"]); // config missing -> ArgumentException
// after
var appId = configuration["Weixin:AppId"];
if (string.IsNullOrWhiteSpace(appId))
throw new InvalidOperationException("Weixin:AppId is not configured.");
var client = new MpApiClient(appId); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(appId))
throw new ArgumentException("AppId must be provided before creating MpApiClient.", nameof(appId));
var client = new MpApiClient(appId); Type guard
bool IsValidAppId(string appId) => !string.IsNullOrWhiteSpace(appId);
Try / catch
try
{
var client = new MpApiClient(appId);
}
catch (ArgumentException ex) when (ex.ParamName == nameof(appId))
{
_logger.LogError(ex, "WeChat MP AppId is missing or empty; check configuration.");
throw new ApplicationException("MP client misconfigured: empty AppId.", ex);
} Prevention
- Validate all WeChat configuration keys at startup with fail-fast checks.
- Bind config into a typed options class with [Required] attributes and validate via IOptions validation.
- Never pass config lookups directly into constructors; read into a variable and check first.
- Add unit tests asserting the client throws on empty appId and that your config loader rejects blanks.
When it happens
Trigger: Calling `new MpApiClient(appId)` with null, an empty string (""), or a string of only whitespace characters (e.g. from an unset config value).
Common situations: AppId read from appsettings.json/environment variables that was never set; DI wiring that passes an unresolved config property; trimming/normalization code that accidentally emptied the value; refactor where the appId field was renamed and the source value lost.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/50fadad5db4c975f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/MpApiClient.cs:33
using System;
using System.Threading;
using System.Threading.Tasks;
using Senparc.Weixin.CommonAPIs.ApiHandlerWapper;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.Containers;
namespace Senparc.Weixin.MP
{
/// <summary>
/// 公众号实例 API 客户端。适合由业务 DI 容器按账号创建,避免修改静态 Service Locator 委托。
/// </summary>
public sealed class MpApiClient
{
public MpApiClient(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.MP,
() => AppId,
AccessTokenContainer.CheckRegistered,
AccessTokenContainer.GetAccessTokenResult,
ApiHandlerWapper.InvalidCredentialValues,View on GitHub (pinned to be573f6f94)