JeffreySu/WeiXinMPSDK · error · ArgumentNullException

ArgumentNullException (options is null)

Error message

ArgumentNullException (options is null)

What it means

MsgAuditFinanceClient's constructor and CreateNativeApi call ValidateOptions, which rejects a null MsgAuditFinanceOptions instance with ArgumentNullException named 'options'. Configuration is mandatory: the client needs CorpId, Secret, timeout, and library path before it can initialize the native SDK.

Solutions

  1. Pass a fully constructed MsgAuditFinanceOptions instance to the constructor
  2. Load options from configuration and assert non-null before constructing the client
  3. If using DI/config binding, verify the configuration section exists and is bound

Example fix

// before
var client = new MsgAuditFinanceClient(config.GetSection("MsgAudit")?.Value == null ? null : BuildOptions());
// after
var options = BuildOptions() ?? throw new InvalidOperationException("MsgAudit finance options missing from config");
var client = new MsgAuditFinanceClient(options);
Defensive patterns

Strategy: validation

Validate before calling

if (options is null) throw new InvalidOperationException("MsgAuditFinanceOptions must be configured before creating MsgAuditFinanceClient");

Type guard

bool HasOptions(MsgAuditFinanceOptions? o) => o is not null;

Try / catch

try { var client = new MsgAuditFinanceClient(options); }
catch (ArgumentNullException ex) when (ex.ParamName == "options") { /* load config and retry */ }

Prevention

When it happens

Trigger: new MsgAuditFinanceClient(null) or constructing the client with an options variable that was never assigned (e.g. DI/binding failure yielding null).

Common situations: Config sections missing so options binding returns null, conditional factory methods returning null on some platforms, or copy-pasted constructor calls passing null explicitly.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/90df99f732a94828. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/MsgAudit/MsgAuditFinanceClient.cs:277

        /// <summary>
        /// 在调用方未显式释放时尽力回收原生资源。
        /// </summary>
        ~MsgAuditFinanceClient()
        {
            ReleaseResources(false);
        }

        private static IMsgAuditFinanceNativeApi CreateNativeApi(MsgAuditFinanceOptions options)
        {
            ValidateOptions(options);
            return new MsgAuditFinanceNativeApi(options.LibraryPath);
        }

        private static void ValidateOptions(MsgAuditFinanceOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ValidateRequiredText(options.CorpId, nameof(options.CorpId));
            ValidateRequiredText(options.Secret, nameof(options.Secret));
            if (options.TimeoutSeconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.TimeoutSeconds),
                    "原生 SDK 网络请求超时时间必须大于 0 秒。");
            }
        }

        private static void ValidateRequiredText(string value, string parameterName)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("参数不能为空。", parameterName);
            }
        }

View on GitHub (pinned to be573f6f94)