JeffreySu/WeiXinMPSDK · error · UnknownRequestMsgTypeException

ResponseMsgType没有为 提供对应处理程序。

Error message

ResponseMsgType没有为 {0} 提供对应处理程序。

What it means

CreateFromRequestMessage maps a ResponseMsgType enum value to a concrete response message class. When the enum value has no mapped case, the default branch throws UnknownRequestMsgTypeException (wrapping ArgumentOutOfRangeException) rather than silently returning a mismatched response type.

Solutions

  1. Pass only supported ResponseMsgType values (Text, Image, News, MpNews, Video, Voice, Music, NoResponse, etc.) or handle those explicitly
  2. When using CreateFromRequestMessage<T>(), ensure the response class name maps cleanly to a ResponseMsgType enum name (the mapping strips the 'ResponseMessage' prefix)
  3. Wrap factory calls in try-catch for UnknownRequestMsgTypeException and fall back to building the response entity manually

Example fix

// before
var msg = WorkResponseMessageBase.CreateFromRequestMessage(request, (ResponseMsgType)99);
// after
ResponseMsgType type = GetSafeResponseType(request);
if (!Enum.IsDefined(typeof(ResponseMsgType), type))
    return new WorkResponseMessageNoResponse();
var msg = WorkResponseMessageBase.CreateFromRequestMessage(request, type);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!Enum.IsDefined(typeof(ResponseMsgType), msgType))
    return new WorkResponseMessageNoResponse();

Type guard

static bool IsSupportedResponseType(ResponseMsgType t) =>
    t is ResponseMsgType.Text or ResponseMsgType.Image or ResponseMsgType.News
        or ResponseMsgType.MpNews or ResponseMsgType.Video or ResponseMsgType.Voice
        or ResponseMsgType.Music or ResponseMsgType.NoResponse;

Try / catch

try { var msg = WorkResponseMessageBase.CreateFromRequestMessage(request, msgType); }
catch (UnknownRequestMsgTypeException ex) { log.Warn(ex.Message); msg = new WorkResponseMessageNoResponse(); }

Prevention

When it happens

Trigger: Passing a ResponseMsgType that has no switch case in WorkResponseMessageBase.CreateFromRequestMessage — e.g. calling the generic CreateFromRequestMessage<T>() where the T type name parses to an unmapped enum member, or a library version where new enum values were added before the mapping was updated.

Common situations: Passing ResponseMsgType.Unknown or Success values to the factory; upgrading the SDK so new enum members exist while custom code paths still hit the old switch; reflection-based generic path with a misspelled response class name.

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/4a3df656749ecb39. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Response/WorkResponseMessageBase.cs:83

						responseMessage = new ResponseMessageNews();
						break;
					case ResponseMsgType.Image:
						responseMessage = new ResponseMessageImage();
						break;
					case ResponseMsgType.Voice:
						responseMessage = new ResponseMessageVoice();
						break;
					case ResponseMsgType.Video:
						responseMessage = new ResponseMessageVideo();
						break;
					case ResponseMsgType.MpNews:
						responseMessage = new ResponseMessageMpNews();
						break;
                    case ResponseMsgType.NoResponse:
                        responseMessage = new WorkResponseMessageNoResponse();
				        break;
					default:
						throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
				}

				responseMessage.ToUserName = requestMessage.FromUserName;
				responseMessage.FromUserName = requestMessage.ToUserName;
				responseMessage.CreateTime = SystemTime.Now; //使用当前最新时间

			}
			catch (Exception ex)
			{
				throw new WeixinException("CreateFromRequestMessage过程发生异常", ex);
			}

			return responseMessage;
		}

		/// <summary>
		/// 获取响应类型实例,并初始化
		/// </summary>

View on GitHub (pinned to be573f6f94)