JeffreySu/WeiXinMPSDK · error · WeixinException

执行WeixinResult时提供的MessageHandler不能为Null!

Error message

执行WeixinResult时提供的MessageHandler不能为Null!

What it means

FixWeixinBugWeixinResult.ExecuteResult (sync, classic ASP.NET MVC) renders either Content or, when Content is null, the FinalResponseDocument produced by an IMessageHandler. If Content is null and no MessageHandler was provided, there is nothing to write to the WeChat response, so it throws WeixinException. This is a developer configuration mistake in wiring the result.

Solutions

  1. Pass a MessageHandler: new FixWeixinBugWeixinResult(customMessageHandler, null) so the response document is generated.
  2. Or set the Content property to a plain string response instead of relying on a handler.
  3. Check the controller action path to make sure the branch that builds the handler actually runs before ExecuteResult.
  4. Catch WeixinException in a global filter to log misconfigured WeChat endpoints.

Example fix

// before
return new FixWeixinBugWeixinResult(null, null); // nothing to render
// after
var handler = new CustomMessageHandler(Request.InputStream, null, 10);
handler.Execute();
return new FixWeixinBugWeixinResult(handler, null);
Defensive patterns

Strategy: try-catch

Validate before calling

if (result.Content == null && result.MessageHandlerDocument == null)
    throw new InvalidOperationException("WeixinResult needs Content or a MessageHandler");

Type guard

static bool CanRender(FixWeixinBugWeixinResult r) =>
    r.Content != null || r.MessageHandlerDocument != null;

Try / catch

try { return result; }
catch (WeixinException ex) { logger.LogError(ex, "WeixinResult misconfigured: no content or handler"); throw; }

Prevention

When it happens

Trigger: Returning new FixWeixinBugWeixinResult() (or with content=null) from an MVC controller action for a WeChat callback without assigning MessageHandler/MessageHandlerDocument.

Common situations: WeChat server-verification/message controller that sets neither Content nor a message handler; refactoring a controller that used to set _messageHandlerDocument; copy-pasted result usage missing the handler argument.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.AspNet/Results/FixWeixinBugWeixinResult.cs:94

                    //    return _messageHandlerDocument.TextResponseMessage;
                    //}
                }
                return null;
            }
            set { base.Content = value; }
        }

#if NET462
        public override void ExecuteResult(ControllerContext context)
        {
            var content = this.Content;

            if (content == null)
            {
                //使用IMessageHandler输出
                if (_messageHandlerDocument == null)
                {
                    throw new Senparc.Weixin.Exceptions.WeixinException("执行WeixinResult时提供的MessageHandler不能为Null!", null);
                }
                var finalResponseDocument = _messageHandlerDocument.FinalResponseDocument;


                if (finalResponseDocument == null)
                {
                    //throw new Senparc.Weixin.MP.WeixinException("FinalResponseDocument不能为Null!", null);
                }
                else
                {
                    content = finalResponseDocument.ToString();
                }
            }

            context.HttpContext.Response.ClearContent();
            context.HttpContext.Response.ContentType = "text/xml";
            content = (content ?? "").Replace("\r\n", "\n");

View on GitHub (pinned to be573f6f94)