tags:

views:

271

answers:

2

In the following scenario I want to return just a string because that's what the spec says but to do that I have to return a stream and I just want to make sure than I don't keep too many streams around for too long. The method looks like:

[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "someuri/{parameter}")]
[OperationContract]
public Stream FooBar(string parameter)
{
    byte[] bytes = Encoding.UTF8.GetBytes("some string");
    return new MemoryStream(bytes);
}

Does anyone know when this resource is released?

+1  A: 

I would think by the GC, as for a normal object: when all references to it have gone.

And that is not bad, MemoryStream does implement IDisposable but doesn't really need it.

Henk Holterman
In fact `MemoryStream`, just as any `Stream`-derived class, implements `IDisposable`.
Anton Gogolev
Anton, I think that is what I said, no?
Henk Holterman
According to nitzmahone and what I can gather from other sources that should be pretty rapidly.
mhenrixon
+2  A: 

I've been doing some research and found a few interesting articles on the topic:

Hope that might be helpful!

marc_s
Henk: the caller (disposer) in this case is the WCF dispatch infrastructure, not the client. With the default operation behavior, the returned Stream object will be disposed when the operation completes (eg, the response has been sent fully or the channel has faulted). The Stream object isn't exposed directly to the client- it's consumed by the WCF dispatch infrastructure and destroyed.
nitzmahone