C# 如何将 MessageBodyStream 转换为 MemoryStream?
我正在从 WCF 服务返回一个 Stream 并尝试将其转换为 MemoryStream。但是在使用 WCF 服务的 Web 应用程序中,我得到了“MessageBodyStream”的结果,其中我期待“System.IO.Stream” ”。我如何将其转换为 MemoryStream ?
I am returning a Stream from a WCF Service and trying to convert it to a MemoryStream.But in the web application where in consume the WCF service,I am getting the result as of "MessageBodyStream" where i was expecting "System.IO.Stream". How can i convert this to a MemoryStream ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要将 MessageBodyStream 转换为 MemoryStream,请按照下列步骤操作:
您就完成了!
希望这有帮助。
To convert MessageBodyStream to a MemoryStream, follow these steps:
And you are done !!
Hope this helps.
有时,流进来时你不知道它们有多大,为此使用以下代码:
然后一旦你有了字节数组,你就可以将其转换为内存流......
Sometimes the streams come in and you dont know how big they are, for that use this code:
Then once you have the byte array you can convert it to a memory stream...
Message.BodyStream
的类型为Stream
。您需要将整个流读入
MemoryStream
才能进行转换。我不知道这是否是您真正想要的,但您可以简单地将值分配给
MemoryStream
变量,单个MemoryStream
继承自Stream.
Message.BodyStream
is of typeStream
.You will need to read the whole stream into your
MemoryStream
in order to convert it.I don't know if this is what you actually want, but you could be simply assign the value to a
MemoryStream
variable, singeMemoryStream
inherits fromStream
.看起来示例代码中的缓冲区每次在内循环中加长时其大小都会加倍。因此,用
加法代替乘法不是更好吗?例如,
理想情况下,我们可以在这里使用变量而不是硬编码值,但我希望这能传达这一点。
Looks like the buffer in example code will double in size each time it is lengthened in the inner loop. So instead of
wouldn't it be better to add instead of multiply, e.g.
Ideally we might use a variable instead of a hard coded value here, but I hope this conveys the point.