JeffreySu/WeiXinMPSDK · error · WeixinException

ResponseMessageBase.CreateFromRequestMessage

Error message

ResponseMessageBase.CreateFromRequestMessage<T>过程发生异常!

What it means

The generic CreateFromRequestMessage<T>() derives the ResponseMsgType from the type name of T (by stripping 'ResponseMessage' and parsing an enum), then delegates to the non-generic factory. Any exception in that flow — invalid enum name derived from T, unmapped msg type, or population failure — is rethrown as a WeixinException with this message and the original exception as InnerException.

Solutions

  1. Ensure T's class name matches the pattern ResponseMessage<EnumName> and that EnumName is a valid ResponseMsgType member, or use the non-generic overload with an explicit msgType
  2. Null-check requestMessage before the call
  3. Catch WeixinException and inspect InnerException for the underlying error

Example fix

// before
var msg = WorkResponseMessageBase.CreateFromRequestMessage<MyCustomResponse>(requestMessage);
// after
var msg = WorkResponseMessageBase.CreateFromRequestMessage(requestMessage, ResponseMsgType.Text);
Defensive patterns

Strategy: try-catch

Validate before calling

var name = typeof(T).Name.Replace("ResponseMessage", "");
if (!Enum.IsDefined(typeof(ResponseMsgType), name))
    throw new InvalidOperationException($"{typeof(T).Name} does not map to a ResponseMsgType");

Type guard

static bool TypeMapsToResponseMsgType<T>() where T : WorkResponseMessageBase =>
    Enum.IsDefined(typeof(ResponseMsgType), typeof(T).Name.Replace("ResponseMessage", ""));

Try / catch

try { var msg = WorkResponseMessageBase.CreateFromRequestMessage<T>(requestMessage); }
catch (WeixinException ex) { log.Error(ex.Message, ex.InnerException); throw; }

Prevention

When it happens

Trigger: Calling CreateFromRequestMessage<SomeResponseMessage>() where the class name minus 'ResponseMessage' does not match a ResponseMsgType enum member, or where the underlying factory throws (e.g. null requestMessage, unmapped type).

Common situations: Custom or renamed response entity classes whose names no longer map to enum members; passing null requestMessage; SDK version drift between entity classes and enum values.

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

Appendix: source

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

		/// <summary>
		/// 获取响应类型实例,并初始化
		/// </summary>
		/// <typeparam name="T">需要返回的类型</typeparam>
		/// <param name="requestMessage">请求数据</param>
		/// <returns></returns>
		public static T CreateFromRequestMessage<T>(IWorkRequestMessageBase requestMessage) where T : WorkResponseMessageBase
		{
			try
			{
				var tType = typeof(T);
				var responseName = tType.Name.Replace("ResponseMessage", ""); //请求名称
				ResponseMsgType msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), responseName);
				return CreateFromRequestMessage(requestMessage, msgType) as T;
			}
			catch (Exception ex)
			{
				throw new WeixinException("ResponseMessageBase.CreateFromRequestMessage<T>过程发生异常!", ex);
			}
		}

		/// <summary>
		/// 从返回结果XML转换成IResponseMessageBase实体类
		/// </summary>
		/// <param name="xml">返回给服务器的Response Xml</param>
		/// <returns></returns>
		public static IWorkResponseMessageBase CreateFromResponseXml(string xml)
		{
			try
			{
				if (string.IsNullOrEmpty(xml))
				{
					return null;
				}

				var doc = XDocument.Parse(xml);

View on GitHub (pinned to be573f6f94)