Stream Reader
-
This does not appear to be working and I can't figure out why
MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;
Blog Have I http:\\www.frankkerrigan.com
-
This does not appear to be working and I can't figure out why
MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;
Blog Have I http:\\www.frankkerrigan.com
Frank Kerrigan wrote:
This does not appear to be working and I can't figure out why
What exactly do you mean by "not working"?
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
Frank Kerrigan wrote:
This does not appear to be working and I can't figure out why
What exactly do you mean by "not working"?
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.
Blog Have I http:\\www.frankkerrigan.com
-
This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.
Blog Have I http:\\www.frankkerrigan.com
This is a lifetime issue because the underlying stream has been finalized before the StreamReader was flushed. This article should help you to get more light into this issue: http://geekswithblogs.net/akraus1/articles/81629.aspx Yours, Alois Kraus
-
This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.
Blog Have I http:\\www.frankkerrigan.com
The reason is because the
using
block closes theStreamReader
when you move out of the scope of theusing
block. When you close aStreamReader
OR aStreamWriter
, however, the underlying stream object is also closed and thus disposed. If your stream was aFileStream
then that might make logical sense but it is not so obvious with aMemoryStream
because you probably want to use it as temporary storage or pass to another method for further processing etc. Even if you did not use theusing
block theMemoryStream
would still be closed the moment theStreamReader
is disposed of or garbage collected. I've no idea why it would just suddenly show up now after so many months - That is a curiosity.
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
Frank Kerrigan wrote:
This does not appear to be working and I can't figure out why
What exactly do you mean by "not working"?
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
Thanks guys
Blog Have I http:\\www.frankkerrigan.com
-
This is a lifetime issue because the underlying stream has been finalized before the StreamReader was flushed. This article should help you to get more light into this issue: http://geekswithblogs.net/akraus1/articles/81629.aspx Yours, Alois Kraus
Alois Kraus wrote:
Sorry, that link doesn't work for me.
*** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos
-
This does not appear to be working and I can't figure out why
MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;
Blog Have I http:\\www.frankkerrigan.com
sr.ReadToEnd will read to the end from the current position. Make sure the position is 0 before reading to end. Also, don't touch the memory stream after it has been disposed. The using(...) block will dispose the object; it will actually compile down to this:
StreamReader sr = null;
try
{
sr = new StreamReader(ms);
message = sr.ReadToEnd();
}
finally
{
if(sr != null)
{
sr.Dispose();
}
}Since it gets disposed when the using block leaves scope, callign ms.Position = 0 after the using scope is going to cause problems.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango