JeffreySu/WeiXinMPSDK · error · ArgumentException

AppKey 不能为空。

Error message

AppKey 不能为空。

What it means

The WorkApiClient constructor throws ArgumentException ('AppKey 不能为空。', paramName 'appKey') when the supplied appKey is null, empty, or whitespace. AppKey is a required immutable property of the client used to authenticate subsequent Execute<T> calls, so the library validates it eagerly at construction time rather than failing later on the first request.

Solutions

  1. Pass a valid, non-empty appKey string to the WorkApiClient constructor.
  2. Check the configuration source: ensure the appKey setting exists in appsettings.json / environment variables and is read correctly.
  3. Fail fast at startup: validate required config values before wiring the client in DI.
  4. Use a factory that throws a descriptive configuration error if appKey is missing.

Example fix

// before
var client = new WorkApiClient(Configuration["WorkAppKey"]); // null -> ArgumentException

// after
var appKey = Configuration["WorkAppKey"];
if (string.IsNullOrWhiteSpace(appKey))
    throw new InvalidOperationException("Missing config: WorkAppKey");
var client = new WorkApiClient(appKey);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(appKey))
    throw new InvalidOperationException("WorkAppKey missing from configuration");
var client = new WorkApiClient(appKey);

Type guard

bool HasAppKey(string key) => !string.IsNullOrWhiteSpace(key);

Try / catch

try { var client = new WorkApiClient(appKey); }
catch (ArgumentException ex) when (ex.ParamName == "appKey")
{
    // config missing WorkAppKey — surface a clear startup error
}

Prevention

When it happens

Trigger: new WorkApiClient(null), new WorkApiClient(""), or new WorkApiClient(" ") — i.e. constructing the client with an appKey that is null/empty/whitespace; typically the appKey came from an uninitialized config value.

Common situations: Configuration key missing in appsettings.json so config read returns null; environment variable not set in the deployment environment; DI container wiring passing a not-yet-populated value; refactoring renamed the config property.

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/2d780ea00da887f9. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/CommonAPIs/WorkApiClient.cs:39

using Senparc.Weixin.Work.Containers;

namespace Senparc.Weixin.Work
{
    /// <summary>
    /// 企业微信实例 API 客户端。适合由业务 DI 容器按应用创建。
    /// </summary>
    public sealed class WorkApiClient
    {
        public WorkApiClient(string corpId, string corpSecret)
            : this(AccessTokenContainer.BuildingKey(corpId, corpSecret))
        {
        }

        public WorkApiClient(string appKey)
        {
            AppKey = !string.IsNullOrWhiteSpace(appKey)
                ? appKey
                : throw new ArgumentException("AppKey 不能为空。", nameof(appKey));
        }

        public string AppKey { get; }

        public T Execute<T>(Func<string, T> operation, bool retryInvalidCredential = true)
            where T : WorkJsonResult, new()
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            return ApiHandlerWapperBase.TryCommonApiBase(
                PlatformType.Work,
                () => AppKey,
                AccessTokenContainer.CheckRegistered,
                AccessTokenContainer.GetTokenResult,
                ApiHandlerWapper.InvalidCredentialValues,

View on GitHub (pinned to be573f6f94)