{"record":{"id":"0e145b8c8e35d4be","repo":"JeffreySu/WeiXinMPSDK","slug":"workmessagehandler-async","errorCode":null,"errorMessage":"没有找到合适的消息类型。","messagePattern":"没有找到合适的消息类型。","errorType":"exception","errorClass":"WeixinException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs","lineNumber":68,"sourceCode":"{\n    public abstract partial class WorkMessageHandler<TMC>\n        : MessageHandler<TMC, IWorkRequestMessageBase, IWorkResponseMessageBase>, IWorkMessageHandler\n        where TMC : class, IMessageContext<IWorkRequestMessageBase, IWorkResponseMessageBase>, new()\n    {\n        public override async Task BuildResponseMessageAsync(CancellationToken cancellationToken)\n        {\n            switch (RequestMessage.MsgType)\n            {\n                case RequestMsgType.Unknown: //第三方回调\n                {\n                    if (RequestMessage is IThirdPartyInfoBase)\n                    {\n                        var thirdPartyInfo = RequestMessage as IThirdPartyInfoBase;\n                        TextResponseMessage = await OnThirdPartyEventAsync(thirdPartyInfo);\n                    }\n                    else\n                    {\n                        throw new WeixinException(\"没有找到合适的消息类型。\");\n                    }\n                }\n                    break;\n                //以下是普通信息\n                case RequestMsgType.Text:\n                {\n                    var requestMessage = RequestMessage as RequestMessageText;\n\n                    ResponseMessage = await OnTextOrEventRequestAsync(requestMessage) ??\n                                      await OnTextRequestAsync(requestMessage);\n                }\n                    break;\n                case RequestMsgType.Location:\n                    ResponseMessage = await OnLocationRequestAsync(RequestMessage as RequestMessageLocation);\n                    break;\n                case RequestMsgType.Image:\n                    ResponseMessage = await OnImageRequestAsync(RequestMessage as RequestMessageImage);\n                    break;","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs#L50-L86","documentation":"WorkMessageHandler's async BuildResponseMessageAsync dispatches on the request message type; when the message implements neither a known normal message type nor a third-party info interface, it cannot build a response and throws WeixinException. This guards the handler against receiving message classes the Work (WeCom) handler does not recognize.","triggerScenarios":"Calling BuildResponseMessageAsync (directly or via message handler execution) with a RequestMessage whose type is not one of the handled MsgTypes and which cannot be cast to IThirdPartyInfoBase — e.g. a custom/unknown message model, or a message type not yet mapped in the switch.","commonSituations":"Feeding an MP (Official Account) message object into the Work handler; a new WeCom callback message type added by Tencent but not yet supported by the installed library version; manually constructing a request message with an unmapped MsgType for unit tests.","solutions":["Check that the incoming message is deserialized as a Senparc.Weixin.Work request message type and routed to WorkMessageHandler, not another platform's handler","Upgrade Senparc.Weixin.Work / Senparc.Weixin.SDK to the latest version so new WeCom message types are supported","Override OnTextOrEventRequestAsync or the relevant On*Request method and/or extend the message-type switch in a derived handler to cover your message type","Log RequestMessage.MsgType and the concrete RequestMessage class to identify the unmapped type before handling"],"exampleFix":"// before (derived handler throws on unknown type)\nvar handler = new WorkMessageHandler<int>(requestMsg, stream, maxRecordSize, postModel);\n// after: pre-check the message type and skip unknown ones\nif (requestMessage.MsgType == RequestMsgType.Unknown) { return; }\nvar handler = new WorkMessageHandler<int>(requestMsg, stream, maxRecordSize, postModel);","handlingStrategy":"try-catch","validationCode":"if (requestMessage == null || requestMessage.MsgType == RequestMsgType.Unknown) { return null; }","typeGuard":"bool IsSupportedWorkMessage(IRequestMessageBase msg) => msg is IRequestMessageText || msg is IRequestMessageEventBase || msg is IRequestMessageImage || msg is IRequestMessageVoice || msg is IRequestMessageVideo || msg is IRequestMessageLocation || msg is IThirdPartyInfoBase;","tryCatchPattern":"try { response = await handler.BuildResponseMessageAsync(); } catch (WeixinException ex) { _logger.LogWarning(ex, \"Unmapped Work message type: {Type}\", requestMessage?.MsgType); response = null; }","preventionTips":["Always route messages to the platform-specific handler (WorkMessageHandler, not MessageHandler)","Keep Senparc.Weixin.Work packages updated for new WeCom message types","Log MsgType and concrete class name when deserializing callbacks","Use supported entity factories (Enlightener) instead of hand-built message objects"],"tags":["wechat-work","message-handler","unknown-message-type"],"backgroundTag":"unsupported-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"}