今天在日志里,看到这样一段错误信息。
System.Web.HttpException (0x80004005): 在调用 HttpRequest.GetBufferlessInputStream 之后,此方法或属性不受支持。
回想到今天刚刚把request, response 的日志打印上线,一看对应的出错位置是获取微信支付回调的body , 也就是说在打印完日志后,又重新读取了一下。
来到了伟大的SO,找到了解决方法。
If you want to read again and again, you would probably want to read
as stream and seek to beginning every time you read the stream. But
then if you want to do what do you now but get the second read
working, you can seek to the beginning of the stream, after the first
read, like this.
await httpContent.LoadIntoBufferAsync();
var X = await httpContent.ReadAsAsync<T>();
Stream stream = await httpContent.ReadAsStreamAsync();
stream.Seek(0, SeekOrigin.Begin);
var Y = await httpContent.ReadAsAsync<Dictionary<string, object>>();
总结:
每次都LoadIntoBufferAsync,每次都回到原位。
本文解决了在使用HttpRequest处理微信支付回调时遇到的重复读取Body的问题。通过使用LoadIntoBufferAsync方法并确保每次读取前都将流指针置于起始位置,实现了多次读取的需求。
664

被折叠的 条评论
为什么被折叠?



