e中读取的正确姿势
e中的虽然是⼀个Stream,但它是⼀个与众不同的Stream——不允许on=0,这就意
味着只能读取⼀次,要想多次读取,需要借助MemoryStream,详见博问
using(varbuffer=newMemoryStream())
{
(buffer);
on=0;
(ream);
ine(":");
on=0;
(andardOutput());
}
昨天读了博⽂之后得知在e2.0中已经针对这个问题提供了解决⽅法——EnableRewind(),只要启⽤倒带功能,就可以
让回归正常Stream。
使⽤⾮常简单,引⽤命名空间al,调⽤⽅法Rewind()即可,下⾯我们⽤简单的⽰例代码
体验⼀下
publicclassHomeController:Controller
{
publicIActionResultIndex()
{
Rewind();
ine("1:");
on=0;
(andardOutput());
ine();
ine("2:");
on=0;
(andardOutput());
ine();
returnOk();
}
}
启动上⾯的e站点,然后⽤curl命令发出请求
curl-XPOST-d'HelloWorld'localhost:5000
控制台就会输出期望的结果
1:
HelloWorld
2:
HelloWorld
EnableRewind有2个参数bufferThreshold与bufferLimit。bufferThreshold设置的是最⼤缓存字节数(默认是30K),超
出这个阈值的字节会被写⼊磁盘;bufferLimit设置的是允许的最⼤字节数(默认值是null),超出这个限制,就会抛出异常
ption。
EnableRewind的实现源代码见
更新:Core3.0中EnableRewind被改名为EnableBuffering。
本文发布于:2022-12-07 05:44:08,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/57726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |