JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException (msgType)

Error message

ArgumentOutOfRangeException (msgType)

What it means

Work (WeCom) ChatApi.SendChatSimpleMessage switches on the ChatMsgType of the message. If msgType is none of text/textmarkdown/image/voice/file (e.g. video or an out-of-range cast), the default branch throws ArgumentOutOfRangeException because the simple-message API only supports those types.

Solutions

  1. Only pass ChatMsgType.text, textmarkdown, image, voice, or file to SendChatSimpleMessage
  2. For video messages call the dedicated SendChatVideoMessage API instead
  3. Validate the enum with Enum.IsDefined before casting from int
  4. Extend/upgrade the SDK if a new msg type exists in the WeCom API but not in the switch

Example fix

// before
await api.SendChatSimpleMessage(token, chatId, ChatMsgType.video, mediaId);
// after
await api.SendChatVideoMessage(token, chatId, mediaId, "video title", "desc");
Defensive patterns

Strategy: validation

Validate before calling

if (msgType is not (ChatMsgType.text or ChatMsgType.textmarkdown or ChatMsgType.image or ChatMsgType.voice or ChatMsgType.file)) throw new InvalidOperationException($"{msgType} not supported by SendChatSimpleMessage");

Type guard

static bool IsSimpleMessageType(ChatMsgType t) => t is ChatMsgType.text or ChatMsgType.textmarkdown or ChatMsgType.image or ChatMsgType.voice or ChatMsgType.file;

Try / catch

try { await api.SendChatSimpleMessage(token, chatId, msgType, content); } catch (ArgumentOutOfRangeException ex) { log.Error("Unsupported msgType", ex); }

Prevention

When it happens

Trigger: Calling SendChatSimpleMessage with ChatMsgType.video (or any value not handled by the switch), or casting an arbitrary int to ChatMsgType that has no matching enum member.

Common situations: Developers assuming the simple API accepts all ChatApi message types (video/revealrefile are separate methods); storing msgType as int in a DB and casting to ChatMsgType after an SDK version added new members.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/Chat/ChatApi.cs:164

            {
                BaseSendChatMessageData data;

                switch (msgType)
                {
                    case ChatMsgType.text:
                        data = new SendTextMessageData(chatId, contentOrMediaId, safe);
                        break;
                    case ChatMsgType.image:
                        data = new SendImageMessageData(chatId, contentOrMediaId, safe);
                        break;
                    case ChatMsgType.voice:
                        data = new SendVoiceMessageData(chatId, contentOrMediaId, safe);
                        break;
                    case ChatMsgType.file:
                        data = new SendFileMessageData(chatId, contentOrMediaId, safe);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("msgType");
                }
                return CommonJsonSend.Send<WorkJsonResult>(accessToken, _urlFormatSend, data, CommonJsonSendType.POST, timeOut);
            }, accessTokenOrAppKey);
        }
        /// <summary>
        /// 发送视频消息
        /// </summary>
        /// <param name="accessTokenOrAppKey">调用接口凭证(AccessToken)或AppKey(根据AccessTokenContainer.BuildingKey(corpId, corpSecret)方法获得)</param>
        /// <param name="chatId">会话id</param>
        /// <param name="media_id">视频媒体文件id</param>
        /// <param name="title">视频消息的标题,不超过128个字节</param>
        /// <param name="description">视频消息的描述,不超过512个字节</param>
        /// <param name="safe">表示是否是保密消息,0表示否,1表示是,默认0</param>
        /// <param name="timeOut">代理请求超时时间(毫秒)</param>
        /// <returns></returns>
        public static WorkJsonResult SendChatVideoMessage(string accessTokenOrAppKey, string chatId, string media_id, string title = null, string description = null, int safe = 0, int timeOut = Config.TIME_OUT)
        {
            return ApiHandlerWapper.TryCommonApi(accessToken =>

View on GitHub (pinned to be573f6f94)