Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with MemoryStream 2

Status
Not open for further replies.

NotMyRealName221

Programmer
Oct 28, 2008
8
0
0
US
Hello,

thanks for taking a look at my problem.

I have a SoapExtension that needs to access the soap message for logging. I'm trying to switch that logging from flat file to database.

Everything has been working fine with the flat file all along. The basic process is this:

Copy the existing message stream.
Write it out to a file
Copy the stream back to the message stream (or else it wont be available when other steps need it)

I'm trying to change that where it writes to the DB instead so the simplest idea seemed to be outputing the stream as a string and using that in the SQL query (parameterized of course)

No matter what I do with that stream once I've output it as a string it's closed, I can't reposition, seek or anything that will make that stream usable again.

Here is my code to convert from Stream to String:
Code:
public string getStringFromStream(Stream stream)
{
  using (StreamReader reader = new StreamReader(stream))
  {
    return = reader.ReadToEnd();
  }
}

With the filelogging code it simply resets the Pointer to 0 when done and everything works fine.

If I try to reset the Pointer to 0 after this function it gives me the Stream Closed error.

Any idea how to get the string from the stream without busting the stream?
Thanks
 
using(stream)
{
}
is the same as
try
{
stream.readtoend();
}
finally
{
stream.dispose();
}
if you attempt to use the stream after this point the exception is thrown.

there are some guidelines for how to dispose of items. if i recall correctly they are:
1. if the client creates the object it should dispose of the object.
2. if the client creates the object from a factory is should also dispose of it.
3. if the client is given the object it should not dispose of it.

so in this case the member should only call
return stream.ReadToEnd();
at that point this member becomes moot.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The using block automatically disposes your reader/stream once it is done reading. If you want to control when it is disposed, don't use the using statement.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top