tags:

views:

53

answers:

5

I'm learning REST to implement some Services with WCF. I implemented an example with a MemoryStream. Because MemoryStream is Disposable I wrapped it in a using. When I do this I sometimes can see the xml response in the browser (IE8) and sometimes it will just show me the following Errormessage:

The download of the specified resource has failed. Error processing resource 'http://localhost:8889/SimpleGetService/'.

Why does this occur? When I don't wrap it in a using, I never seem to get the error.

A: 

Sounds like it is being disposed before you stream it back to the response. Try getting the bytes from it as a byte array before disposing it and return the bytes.

Adam
Thanks, this actually worked too, but it is rather inefficient because the data has to be processed twice.
Mark
A: 

In reality there is absolutely no need to Dispose a MemoryStream as it only uses managed resources. It inherits from Stream so it automatically gets the IDisposable interface, but it is not required.

Darrel Miller
+1  A: 

When you return a Stream from a WCF REST service, don't dispose it (since it'll be disposed before the caller can ever do anything with it), just let the framework dispose it for you.

James Manning
A: 

It seems to me that since the MemoryStream implements IDisposable, you should dispose it. Sure, it doesn't do anything TODAY, but it certainly could soemday!

You don't want your code to know the nitty-gritty details of making the REST call so maybe you should pull what you need out of the MemoryStream before the end of the using block and return that to your caller. That's more loosely-coupled anyway - returning a MemoryStream to your caller exposes too much of the details of the implementation IMHO.

n8wrl
In theory yes, in practice it will never happen. Microsoft refuse to fix blatant bugs in the .net framework for fear of breaking existing code. Making memory stream use an unmanaged resource would not only be monumentally stupid, but it would potential break many applications.
Darrel Miller
What datatype would you suggest the OP use to return from the request that would reduce the client-server coupling?
Darrel Miller
I would suggest a datatype that represents the structure of the data he is querying for without exposing the way he got it. For example - I do a CRM query for a customer in one of my apps and I return a 'Customer DTO' object with first name, last name, etc. NOT the XML stream I got it from
n8wrl
+1  A: 

You can set the OperationBehaviorAttribute.AutoDisposeParameters to true. This will take of your problem and dispose all returned objects.

Edit: Btw: this is set to true by default. So you should not have to worry about it at all anyway :)

Flo
Hmm this seems to answer my question, why I don't have to dispose the Stream.. Though I found this here, which allows me to close dispose my disposable objects manualy: http://devdump.wordpress.com/2008/12/07/disposing-return-values/
Mark
Why would you want to close/dispose objects manually when it's already done for you?
Flo
Well given my scenario, there is no need to close the stream manually, but this could come in quite handy when I feel the need to dispose my stuff on a certain event and/or have disabled the autodisposeparameters..
Mark