Request.Content读取2次

今天在日志里,看到这样一段错误信息。

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,每次都回到原位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值