JeffreySu/WeiXinMPSDK · error · UnknownRequestMsgTypeException

MsgType: 在RequestMessageFactory中没有对应的处理程序!

Error message

MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!

What it means

MessageHandler.OnUnknownTypeRequest throws UnknownRequestMsgTypeException when WeChat sends a message whose MsgType has no registered handler in RequestMessageFactory. It is deliberately thrown to fail fast on unknown/undocumented message types (e.g. new WeChat message categories not yet supported by the SDK version), as the library cannot map them to a RequestMessage type. The exception message embeds the raw MsgType string parsed from the request XML document.

Solutions

  1. Upgrade Senparc.Weixin / Senparc.Weixin.MP NuGet packages to the latest version so the new MsgType is recognized by RequestMessageFactory.
  2. Override OnUnknownTypeRequest in your custom MessageHandler to return a fallback response (e.g. empty or 'success') instead of letting the base implementation throw, so unknown types are tolerated.
  3. Log msgType from the exception message and inspect the raw request XML (RequestDocument) to identify what WeChat actually sent.
  4. Wrap the Execute/async handling pipeline in try-catch for UnknownRequestMsgTypeException as the library comment suggests, to keep the WeChat server-side exchange from failing.

Example fix

// before (default behavior)
// OnUnknownTypeRequest throws UnknownRequestMsgTypeException

// after: tolerate unknown types in your handler
public override IResponseMessageBase OnUnknownTypeRequest(RequestMessageUnknownType requestMessage)
{
    var msgType = requestMessage.RequestDocument.Root.Element("MsgType")?.Value;
    Log.Warn("Unhandled MsgType: " + msgType);
    return RequestMessageFactory.CreateResponseMessage<ResponseMessageText>(this); // or null/'success'
}
Defensive patterns

Strategy: try-catch

Validate before calling

var msgType = doc.Root?.Element("MsgType")?.Value;
if (!supportedMsgTypes.Contains(msgType))
    Log.Warn("Unknown MsgType from WeChat: " + msgType);

Type guard

bool IsKnownMsgType(string msgType) =>
    new[] { "text","image","voice","video","location","link","event","shortvideo" }.Contains(msgType);

Try / catch

try
{
    await messageHandler.ExecuteAsync(cancellationToken);
}
catch (UnknownRequestMsgTypeException ex)
{
    logger.LogWarning(ex, "Unhandled WeChat MsgType");
    return new ContentResult { Content = "success" }; // ack to WeChat
}

Prevention

When it happens

Trigger: WeChat server pushes a message with a MsgType string that RequestMessageFactory.GetRequestEntity cannot resolve (e.g. a newly introduced WeChat message type like 'miniprogrampage' or an undocumented type such as 'subscribe' events handled elsewhere), while a custom MessageHandler does not override OnUnknownTypeRequest; MessageHandler.Execute then calls OnUnknownTypeRequest via OnUnknownTypeRequestAsync.

Common situations: Developers running an older Senparc.Weixin SDK version against a newer WeChat API that introduced new MsgType values; typos or custom MsgTypes in test pushes; handlers that rely on the default factory without upgrading packages after WeChat adds a message category.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.MP/Senparc.Weixin.MP/MessageHandlers/MessageHandler.Message.cs:74

        /// 默认返回消息(当任何OnXX消息没有被重写,都将自动返回此默认消息)
        /// </summary>
        public abstract IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage);
        //{
        //    例如可以这样实现:
        //    var responseMessage = this.CreateResponseMessage<ResponseMessageText>();
        //    responseMessage.Content = "您发送的消息类型暂未被识别。";
        //    return responseMessage;
        //}

        /// <summary>
        /// 未知类型消息触发的事件,默认将抛出异常,建议进行重写
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public virtual IResponseMessageBase OnUnknownTypeRequest(RequestMessageUnknownType requestMessage)
        {
            var msgType = MsgTypeHelper.GetRequestMsgTypeString(requestMessage.RequestDocument);
            throw new UnknownRequestMsgTypeException("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!".FormatWith(msgType), new ArgumentOutOfRangeException());//为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
        }

        #endregion

        #region 接收消息方法


        /// <summary>
        /// 预处理文字或事件类型请求。
        /// 这个请求是一个比较特殊的请求,通常用于统一处理来自文字或菜单按钮的同一个执行逻辑,
        /// 会在执行OnTextRequest或OnEventRequest之前触发,具有以下一些特征:
        /// 1、如果返回null,则继续执行OnTextRequest或OnEventRequest
        /// 2、如果返回不为null,则终止执行OnTextRequest或OnEventRequest,返回最终ResponseMessage
        /// 3、如果是事件,则会将RequestMessageEvent自动转为RequestMessageText类型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey
        /// </summary>
        public virtual IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage)
        {
            return null;

View on GitHub (pinned to be573f6f94)