Closed zzj19930721 closed 3 years ago
`
`
你是不是已经把这个流读取了,但是没充值Position,导致后面读取不到了?
遇到了同样的问题,我的问题是因为一些中间件导致的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);`
建议你检查一下自己的中间件
@hongduo168 对,一般是这个问题,Body提前被使用了,但是指针没有回去。
` WeixinController.Post 中: //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.GetRequestMemoryStream(), postModel, maxRecordCount);
报错:Root element is missing.”
但是 Request.GetRequestMemoryStream() 是有值的
`
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.MessageHandler
3..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)