Josh
Josh

Reputation: 13818

Unable to cast object of type 'MessageBodyStream' to type 'System.IO.MemoryStream'

I am returning a memorystream in the form of a Stream from my wcf server. When I retrieve that in the client and cast it back to MemoryStream,I get this error.

I don't understand from where MessageBodyStream came from as I never used it.Can someone please tell me a solution for this problem?

Thank you.

Upvotes: 3

Views: 7774

Answers (3)

Andrew Savinykh
Andrew Savinykh

Reputation: 26290

This post suggests, that you need to read this stream into your MemoryStream first, if you want to access it as MemoryStream. Because any stream you send is received as MessageBodyStream.

Upvotes: 3

driis
driis

Reputation: 164301

MessageBodyStream and MemoryStream are unrelated types, you cant cast one to the other. You should propably just use the returned object as a Stream.

Upvotes: 2

Lucero
Lucero

Reputation: 60190

Why not use it as a Stream instead of a MemoryStream?

You may want to read this blog post, which specifically mentions the behavior you're seeing:

For example, if you send a MemoryStream, the receiver will receive it as the System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream class (any stream you send is received as this).

http://christopherdeweese.com/blog2/post/streaming-in-wcf-knowing-is-half-the-battle

Here's some MSDN info on the topic: http://msdn.microsoft.com/en-us/library/ms733742.aspx

Upvotes: 7

Related Questions