我试图将一个字符串写入内存流,
但失败并出现错误消息:
Memory stream is not expandable.
产生此问题的代码行:
context.Response.Filter = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myPage));
有人有解决方法/修复吗?
堆栈跟踪:
[NotSupportedException: Memory stream is not expandable.]
System.IO.MemoryStream.set_Capacity(Int32 value) +9385744
System.IO.MemoryStream.EnsureCapacity(Int32 value) +50
System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +265
System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +9155697
System.Web.HttpResponse.FilterOutput() +159
System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +52
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
以下代码对我来说工作正常
public class Foo
{
public static void Main()
{
var myPage = "test string";
var repo = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myPage));
}
}
看来正确的方法是创建MemoryStream
使用默认构造函数
var repo = new System.IO.MemoryStream();
然后写入它
var stringBytes = System.Text.Encoding.UTF8.GetBytes(myPage);
repo.Write(stringBytes, 0, stringBytes.Length);
如果您希望能够正常读取流(例如使用 StreamReader),那么您还需要调用:
repo.Seek(0, SeekOrigin.Begin);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)