C# 如何将 MessageBodyStream 转换为 MemoryStream?

发布于 2024-10-06 23:04:55 字数 152 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二手情话 2024-10-13 23:04:55

要将 MessageBodyStream 转换为 MemoryStream,请按照下列步骤操作:

MemoryStream stream = new MemoryStream();
messageStream.CopyTo(stream); // Assuming messageStream is your MessageBodyStream
stream.Position = 0; // Be sure to set the position to 0 before using it.

您就完成了!

希望这有帮助。

To convert MessageBodyStream to a MemoryStream, follow these steps:

MemoryStream stream = new MemoryStream();
messageStream.CopyTo(stream); // Assuming messageStream is your MessageBodyStream
stream.Position = 0; // Be sure to set the position to 0 before using it.

And you are done !!

Hope this helps.

郁金香雨 2024-10-13 23:04:55

有时,流进来时你不知道它们有多大,为此使用以下代码:

public static byte[] ReadToEnd(System.IO.Stream stream)
{
    long originalPosition = stream.Position;

    byte[] readBuffer = new byte[4096];

    int totalBytesRead = 0;
    int bytesRead;

    while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
    {
        totalBytesRead += bytesRead;

        if (totalBytesRead == readBuffer.Length)
        {
            int nextByte = stream.ReadByte();
            if (nextByte != -1)
            {
                byte[] temp = new byte[readBuffer.Length * 2];
                Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                readBuffer = temp;
                totalBytesRead++;
            }
        }
    }

    byte[] buffer = readBuffer;
    if (readBuffer.Length != totalBytesRead)
    {
        buffer = new byte[totalBytesRead];
        Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
    }
    return buffer;
}

然后一旦你有了字节数组,你就可以将其转换为内存流......

    byte[] myBytes = ReadToEnd(theStream);
    Stream theMemStream = new MemoryStream(myBytes, 0, myBytes.Length);

Sometimes the streams come in and you dont know how big they are, for that use this code:

public static byte[] ReadToEnd(System.IO.Stream stream)
{
    long originalPosition = stream.Position;

    byte[] readBuffer = new byte[4096];

    int totalBytesRead = 0;
    int bytesRead;

    while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
    {
        totalBytesRead += bytesRead;

        if (totalBytesRead == readBuffer.Length)
        {
            int nextByte = stream.ReadByte();
            if (nextByte != -1)
            {
                byte[] temp = new byte[readBuffer.Length * 2];
                Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                readBuffer = temp;
                totalBytesRead++;
            }
        }
    }

    byte[] buffer = readBuffer;
    if (readBuffer.Length != totalBytesRead)
    {
        buffer = new byte[totalBytesRead];
        Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
    }
    return buffer;
}

Then once you have the byte array you can convert it to a memory stream...

    byte[] myBytes = ReadToEnd(theStream);
    Stream theMemStream = new MemoryStream(myBytes, 0, myBytes.Length);
鹿港巷口少年归 2024-10-13 23:04:55

Message.BodyStream 的类型为Stream

您需要将整个流读入 MemoryStream 才能进行转换。

我不知道这是否是您真正想要的,但您可以简单地将值分配给 MemoryStream 变量,单个 MemoryStream 继承自 Stream.

Message.BodyStream is of type Stream.

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, singe MemoryStream inherits from Stream.

魔法少女 2024-10-13 23:04:55

看起来示例代码中的缓冲区每次在内循环中加长时其大小都会加倍。因此,用

byte[] temp = new byte[readBuffer.Length * 2] 

加法代替乘法不是更好吗?例如,

byte[] temp = new byte[readBuffer.Length + 4096]

理想情况下,我们可以在这里使用变量而不是硬编码值,但我希望这能传达这一点。

Looks like the buffer in example code will double in size each time it is lengthened in the inner loop. So instead of

byte[] temp = new byte[readBuffer.Length * 2] 

wouldn't it be better to add instead of multiply, e.g.

byte[] temp = new byte[readBuffer.Length + 4096]

Ideally we might use a variable instead of a hard coded value here, but I hope this conveys the point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文