Does a stream close when you return it in a WebOperationContext?

922 views Asked by At

I'm returning some images as a stream as part of a WebOperationContext.Current.CreateStreamResponse()

I was wondering if the framework handles the closing of this stream after it gets returned. Or if there was something that you have to do as part of the response in order to close the stream correctly.

Thanks.

2

There are 2 answers

0
Vilsad P P On

Implementing using is not a soulution here, i had a situation where the returning content was huge (3 MB) and because i wrapped the WebOperationContext.Current.CreateStreamResponse() inside a using statement, the stream got closed before the content finished, thus the response was blank for the client.

All i did was copy the stream output to a string and use WebOperationContext.Current.CreateTextResponse() inside the using wrap.

0
SouthShoreAK On

Well, from what I found, CreateStreamResponse() returns a Message, which implements IDisposable, so it would be best practice to wrap your returned object in a using statement. That should handle closing the stream properly.