JeffreySu / WeiXinMPSDK

微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
https://weixin.senparc.com
Apache License 2.0
8.48k stars 4.36k forks source link

WeixinController.Post 消息处理报:Root element is missing. #2015

Closed zzj19930721 closed 3 years ago

zzj19930721 commented 5 years ago

` WeixinController.Post 中: //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.GetRequestMemoryStream(), postModel, maxRecordCount);

报错:Root element is missing.”

但是 Request.GetRequestMemoryStream() 是有值的

  名称 类型
  CanRead true bool
  CanSeek true bool
  CanTimeout false bool
  CanWrite true bool
  Capacity 350 int
  Length 350 long
  Position 0 long

`

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.Xml.XmlException: Root element is missing. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at Senparc.CO2NET.Utilities.XmlUtility.Convert(Stream stream) at Senparc.NeuChar.MessageHandlers.MessageHandler3..ctor(Stream inputStream, IEncryptPostModel postModel, Int32 maxRecordCount) at Senparc.Weixin.MP.MessageHandlers.MessageHandler1..ctor(Stream inputStream, PostModel postModel, Int32 maxRecordCount, DeveloperInfo developerInfo) in D:\360MoveData\Users\zzj\Desktop\core\WeiXinMPSDK\src\Senparc.Weixin.MP\Senparc.Weixin.MP\MessageHandlers\MessageHandler.cs:line 175 at Senparc.Weixin.MP.Sample.CommonService.CustomMessageHandler.CustomMessageHandler..ctor(Stream inputStream, PostModel postModel, Int32 maxRecordCount) in D:\360MoveData\Users\zzj\Desktop\core\WeiXinMPSDK\Samples\net45-mvc\Senparc.Weixin.MP.Sample.CommonService\MessageHandlers\CustomMessageHandler\CustomMessageHandler.cs:line 87 at Senparc.Weixin.MP.CoreSample.Controllers.WeixinController.Post(PostModel postModel) in D:\360MoveData\Users\zzj\Desktop\core\WeiXinMPSDK\Samples\netcore2.2-mvc\Senparc.Weixin.MP.CoreSample\Controllers\WeixinController.cs:line 98 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

zzj19930721 commented 5 years ago

`

Text 637078778549885700 1572253022

`

JeffreySu commented 4 years ago

你是不是已经把这个流读取了,但是没充值Position,导致后面读取不到了?

hongduo168 commented 4 years ago

遇到了同样的问题,我的问题是因为一些中间件导致的Request.Body内容清空了,解决办法是: `var requestBodyStream = new System.IO.MemoryStream();

        var requestBodyStream = new System.IO.MemoryStream();

        await context.Request.Body.CopyToAsync(requestBodyStream);

        requestBodyStream.Seek(0, System.IO.SeekOrigin.Begin);

        requestBodyStream.Seek(0, System.IO.SeekOrigin.Begin);

        //中间件代码逻辑

        context.Request.Body = requestBodyStream;
        await _next(context);`

建议你检查一下自己的中间件

JeffreySu commented 4 years ago

@hongduo168 对,一般是这个问题,Body提前被使用了,但是指针没有回去。