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.
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
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.
MessageBodyStream
and MemoryStream
are unrelated types, you cant cast one to the other. You should propably just use the returned object as a Stream
.
精彩评论