{"record":{"id":"e1c852a942f26e52","repo":"JeffreySu/WeiXinMPSDK","slug":"argumentoutofrangeexception-msgtype","errorCode":null,"errorMessage":"ArgumentOutOfRangeException (msgType)","messagePattern":"ArgumentOutOfRangeException \\(msgType\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/Chat/ChatApi.cs","lineNumber":164,"sourceCode":"            {\n                BaseSendChatMessageData data;\n\n                switch (msgType)\n                {\n                    case ChatMsgType.text:\n                        data = new SendTextMessageData(chatId, contentOrMediaId, safe);\n                        break;\n                    case ChatMsgType.image:\n                        data = new SendImageMessageData(chatId, contentOrMediaId, safe);\n                        break;\n                    case ChatMsgType.voice:\n                        data = new SendVoiceMessageData(chatId, contentOrMediaId, safe);\n                        break;\n                    case ChatMsgType.file:\n                        data = new SendFileMessageData(chatId, contentOrMediaId, safe);\n                        break;\n                    default:\n                        throw new ArgumentOutOfRangeException(\"msgType\");\n                }\n                return CommonJsonSend.Send<WorkJsonResult>(accessToken, _urlFormatSend, data, CommonJsonSendType.POST, timeOut);\n            }, accessTokenOrAppKey);\n        }\n        /// <summary>\n        /// 发送视频消息\n        /// </summary>\n        /// <param name=\"accessTokenOrAppKey\">调用接口凭证（AccessToken）或AppKey（根据AccessTokenContainer.BuildingKey(corpId, corpSecret)方法获得）</param>\n        /// <param name=\"chatId\">会话id</param>\n        /// <param name=\"media_id\">视频媒体文件id</param>\n        /// <param name=\"title\">视频消息的标题，不超过128个字节</param>\n        /// <param name=\"description\">视频消息的描述，不超过512个字节</param>\n        /// <param name=\"safe\">表示是否是保密消息，0表示否，1表示是，默认0</param>\n        /// <param name=\"timeOut\">代理请求超时时间（毫秒）</param>\n        /// <returns></returns>\n        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)\n        {\n            return ApiHandlerWapper.TryCommonApi(accessToken =>","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/Chat/ChatApi.cs#L146-L182","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only pass ChatMsgType.text, textmarkdown, image, voice, or file to SendChatSimpleMessage","For video messages call the dedicated SendChatVideoMessage API instead","Validate the enum with Enum.IsDefined before casting from int","Extend/upgrade the SDK if a new msg type exists in the WeCom API but not in the switch"],"exampleFix":"// before\nawait api.SendChatSimpleMessage(token, chatId, ChatMsgType.video, mediaId);\n// after\nawait api.SendChatVideoMessage(token, chatId, mediaId, \"video title\", \"desc\");","handlingStrategy":"validation","validationCode":"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\");","typeGuard":"static bool IsSimpleMessageType(ChatMsgType t) => t is ChatMsgType.text or ChatMsgType.textmarkdown or ChatMsgType.image or ChatMsgType.voice or ChatMsgType.file;","tryCatchPattern":"try { await api.SendChatSimpleMessage(token, chatId, msgType, content); } catch (ArgumentOutOfRangeException ex) { log.Error(\"Unsupported msgType\", ex); }","preventionTips":["Route video/revealrefile messages to their dedicated ChatApi methods","Never cast raw ints to ChatMsgType without Enum.IsDefined"],"tags":["enum","argument","wecom"],"backgroundTag":"invalid-enum-value","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"}