JeffreySu/WeiXinMPSDK · error · ArgumentException

不能为空。

Error message

 不能为空。

What it means

The Require helper in CustomsRequestData throws ArgumentException when a required string parameter for a WeChat Pay customs (报关) request is null, empty, or whitespace. The message is '<parameterName> 不能为空。'. It is the library's way of enforcing mandatory fields before a request is signed and sent.

Solutions

  1. Find the parameterName in the exception message and set that property to a non-empty value before calling the declare/query API.
  2. Verify the field against WeChat Pay's customs declaration documentation to confirm the exact required value format.
  3. Add a pre-call validation/checks so missing fields fail fast in your own code.

Example fix

// before
var data = new CustomsDeclareRequestData(key, appid, mch_id, null /* mch_customs_no */, out_trade_no, ...);
// after
var data = new CustomsDeclareRequestData(key, appid, mch_id, "YES customs code", out_trade_no, ...);
Defensive patterns

Strategy: validation

Validate before calling

// C#
if (string.IsNullOrWhiteSpace(data.MchCustomsNo)) throw new InvalidOperationException("mch_customs_no is required before calling the customs API");

Type guard

bool HasValue(string s) => !string.IsNullOrWhiteSpace(s);

Try / catch

try { await api.CallAsync(data); }
catch (ArgumentException ex) when (ex.Message.EndsWith("不能为空。")) { log.LogError(ex, "Missing customs request field: {Param}", ex.ParamName); }

Prevention

When it happens

Trigger: Calling Validate() on any CustomsRequestData subclass (e.g. the Declare/Query/Redeclare request data) without setting a required field — e.g. mch_customs_no, out_trade_no, etc. — leaves the field null/empty and Require fires during Validate.

Common situations: Developers construct the customs request object but forget to assign one field (often mch_customs_no or a merchant order number), or copy sample code that leaves optional-looking fields unassigned when they are actually required, or deserialize config from JSON where the key was missing.

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/39fe167df164d466. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPay/V3/Universal/Customs/CustomsRequestData.cs:75

            Require(mch_id, "mch_id");
            Require(customs, "customs");
            Require(key, "key");
        }

        /// <summary>向签名处理器写入公共字段。</summary>
        protected virtual void SetParameters(RequestHandler handler)
        {
            handler.SetParameter("appid", appid);
            handler.SetParameter("mch_id", mch_id);
            handler.SetParameter("customs", customs);
        }

        /// <summary>校验字符串必填参数。</summary>
        protected static void Require(string value, string parameterName)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException(parameterName + " 不能为空。", parameterName);
            }
        }

        /// <summary>按不变区域格式写入可选整数。</summary>
        protected static void SetOptionalInt(RequestHandler handler,
            string name, int? value)
        {
            if (value.HasValue)
            {
                handler.SetParameter(name,
                    value.Value.ToString(CultureInfo.InvariantCulture));
            }
        }
    }

    /// <summary>
    /// 支付订单海关报关请求。
    /// <para>官方文档:https://pay.weixin.qq.com/doc/v3/merchant/4011985151</para>

View on GitHub (pinned to be573f6f94)