JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

sendType

Error message

sendType

What it means

CommonJsonSend.Send dispatches on the CommonJsonSendType parameter. The switch's default arm throws ArgumentOutOfRangeException("sendType") when sendType is a value outside the implemented Post/Get cases (or an undefined enum cast).

Solutions

  1. Only pass CommonJsonSendType.Post or CommonJsonSendType.Get (values implemented by Send); validate/whitelist before casting from ints or strings.
  2. Use Enum.TryParse<CommonJsonSendType> and check the parsed value is a defined, supported member before calling.
  3. If a newer enum value is needed, upgrade the library or send the request directly via Senparc.Weixin.HttpUtility/RequestUtility instead.
  4. Log the offending sendType value to find where the bad value originates.

Example fix

// before
var type = (CommonJsonSendType)int.Parse(config["sendType"]);
CommonJsonSend.Send<T>(url, data, type);
// after
if (!Enum.TryParse(config["sendType"], out CommonJsonSendType type)
    || (type != CommonJsonSendType.Post && type != CommonJsonSendType.Get))
{
    type = CommonJsonSendType.Post; // or reject the config
}
CommonJsonSend.Send<T>(url, data, type);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(CommonJsonSendType), sendType))
    throw new ArgumentException($"Unsupported sendType: {sendType}");
if (sendType != CommonJsonSendType.Post && sendType != CommonJsonSendType.Get)
    throw new ArgumentException($"Send does not implement {sendType}");

Type guard

bool IsSupportedSendType(CommonJsonSendType t) =>
    t == CommonJsonSendType.Post || t == CommonJsonSendType.Get;

Try / catch

try
{
    CommonJsonSend.Send<T>(url, data, sendType);
}
catch (ArgumentOutOfRangeException)
{
    Log($"sendType {sendType} not supported by Send; falling back to Post.");
    CommonJsonSend.Send<T>(url, data, CommonJsonSendType.Post);
}

Prevention

When it happens

Trigger: Calling CommonJsonSend.Send with sendType = (CommonJsonSendType)99 (undefined), or a library version where a sendType value exists in the enum but Send's switch lacks its case.

Common situations: Reading sendType from config/appsettings as an int and casting without validating; upgrading/downgrading Senparc.Weixin versions where enum members and switch cases diverge; copy-pasting code that used a value valid only for SendAsync or another overload.

Understand the failure class

Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin/Senparc.Weixin/CommonAPIs/CommonJsonSend.cs:219

                            {
                                writer.Write(jsonString);
                                writer.Flush();
                            }
                            ms.Seek(0, SeekOrigin.Begin);

                            WeixinTrace.SendApiPostDataLog(url, jsonString);//记录Post的Json数据

                            //PostGetJson方法中将使用WeixinTrace记录结果
                            return Post.PostGetJson<T>(CommonDI.CommonSP, url, null, ms,
                                timeOut: timeOut,
                                contentType: contentType,
                                afterReturnText: postFailAction,
                                checkValidationResult: checkValidationResult);
                        }

                    //TODO:对于特定的错误类型自动进行一次重试,如40001(目前的问题是同样40001会出现在不同的情况下面)
                    default:
                        throw new ArgumentOutOfRangeException("sendType");
                }
            }
            catch (ErrorJsonResultException ex)
            {
                ex.Url = urlFormat;
                throw;
            }
        }

        #endregion


        #region 异步方法

        /// <summary>
        /// 向需要AccessToken的API发送消息的公共方法
        /// </summary>
        /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>

View on GitHub (pinned to be573f6f94)