{"record":{"id":"2d780ea00da887f9","repo":"JeffreySu/WeiXinMPSDK","slug":"appkey","errorCode":null,"errorMessage":"AppKey 不能为空。","messagePattern":"AppKey 不能为空。","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.Work/Senparc.Weixin.Work/CommonAPIs/WorkApiClient.cs","lineNumber":39,"sourceCode":"using Senparc.Weixin.Work.Containers;\n\nnamespace Senparc.Weixin.Work\n{\n    /// <summary>\n    /// 企业微信实例 API 客户端。适合由业务 DI 容器按应用创建。\n    /// </summary>\n    public sealed class WorkApiClient\n    {\n        public WorkApiClient(string corpId, string corpSecret)\n            : this(AccessTokenContainer.BuildingKey(corpId, corpSecret))\n        {\n        }\n\n        public WorkApiClient(string appKey)\n        {\n            AppKey = !string.IsNullOrWhiteSpace(appKey)\n                ? appKey\n                : throw new ArgumentException(\"AppKey 不能为空。\", nameof(appKey));\n        }\n\n        public string AppKey { get; }\n\n        public T Execute<T>(Func<string, T> operation, bool retryInvalidCredential = true)\n            where T : WorkJsonResult, new()\n        {\n            if (operation == null)\n            {\n                throw new ArgumentNullException(nameof(operation));\n            }\n\n            return ApiHandlerWapperBase.TryCommonApiBase(\n                PlatformType.Work,\n                () => AppKey,\n                AccessTokenContainer.CheckRegistered,\n                AccessTokenContainer.GetTokenResult,\n                ApiHandlerWapper.InvalidCredentialValues,","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.Work/Senparc.Weixin.Work/CommonAPIs/WorkApiClient.cs#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a valid, non-empty appKey string to the WorkApiClient constructor.","Check the configuration source: ensure the appKey setting exists in appsettings.json / environment variables and is read correctly.","Fail fast at startup: validate required config values before wiring the client in DI.","Use a factory that throws a descriptive configuration error if appKey is missing."],"exampleFix":"// before\nvar client = new WorkApiClient(Configuration[\"WorkAppKey\"]); // null -> ArgumentException\n\n// after\nvar appKey = Configuration[\"WorkAppKey\"];\nif (string.IsNullOrWhiteSpace(appKey))\n    throw new InvalidOperationException(\"Missing config: WorkAppKey\");\nvar client = new WorkApiClient(appKey);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(appKey))\n    throw new InvalidOperationException(\"WorkAppKey missing from configuration\");\nvar client = new WorkApiClient(appKey);","typeGuard":"bool HasAppKey(string key) => !string.IsNullOrWhiteSpace(key);","tryCatchPattern":"try { var client = new WorkApiClient(appKey); }\ncatch (ArgumentException ex) when (ex.ParamName == \"appKey\")\n{\n    // config missing WorkAppKey — surface a clear startup error\n}","preventionTips":["Read appKey from strongly-typed configuration with required-field validation at startup","Bind settings via IOptions with ValidateDataAnnotations to catch missing keys early","Never construct API clients inline from unvalidated config strings","Check that environment variables are present in all deployment targets"],"tags":["argument","null","constructor","config","wechat-work"],"backgroundTag":"missing-required-argument","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}