簡體   English   中英

MSMQ + C#,接收帶有編碼字節 [] 正文的消息在 Windows 7 與 Windows XP 上的行為不同

[英]MSMQ + C#, receiving a message with encoded byte[] body behaves differently on Windows 7 vs. Windows XP

我有一個顯示 MSMQ 消息隊列中消息內容的應用程序。 Windows 7 上的 MSMQ 存在問題,沒有保留消息正文中數據的真正 object 類型。 在示例中,我發送了一個 byte[],然后當我收到它時,它不再是一個字節數組,而是包裝好的 XML 容器文檔。 在 Windows XP 中我從來沒有遇到過這個問題,並且 Message.Body 屬性始終正確設置為字節 []。

這是壓縮代碼:

    public void SendWithCompression(string Message)
    {
        try
        {
            // Compress the message data in memory.
            byte[] UncompressedData = Encoding.UTF8.GetBytes(Message);
            byte[] CompressedData = null;

            MemoryStream s = new MemoryStream();
            using (GZipStream z = new GZipStream(s, CompressionMode.Compress))
            {
                z.Write(UncompressedData, 0, UncompressedData.Length);
            }
            CompressedData = s.ToArray();

            if (_Transaction != null)
            {
                _Transaction.Begin();
                base.Send(CompressedData, _Transaction);
                _Transaction.Commit();
            }
            else
            {
                base.Send(CompressedData);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

這是測試消息的消息內容。 It ends up as an XML document that wraps the encoded binary data.: <?xml version="1.0"?>..<base64Binary>H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ih63edMefTsvy2rv8V3+4/8ByygBlxMAAAA=</base64Binary>


這是它使用的解壓代碼:

        String Data;
        //Get message and format XML
        System.Messaging.Message m = MessagesList[ListIndex].Tag;
        m.Formatter = new XmlMessageFormatter(new Type[] 
        {
            typeof(string), // Send raw plain strings
            typeof(byte[]), // Array of binary data
            typeof(XmlNode) // Xml document
        });

        m.BodyStream.Position = 0;

        Data = m.Body.ToString();
            if (m.Body.GetType() == typeof(byte[]))
            {
                try
                {
                    // The message body is an array of binary data.
                    // Assume it is a GZIP stream of compressed data.
                    byte[] CompressedData = (byte[])m.Body;
                    byte[] UncompressedData = null;

                    // Decompress it.
                    MemoryStream s = new MemoryStream(CompressedData);
                    using (GZipStream z = new GZipStream(s, CompressionMode.Decompress))
                    {
                        UncompressedData = MyExtensions.ReadRemainingBytes(z);
                    }

                    // Turn the bytes back into a string.
                    Data = Encoding.UTF8.GetString(UncompressedData);
                }
                catch
                {
                    Data = "Unknown binary data: " +
                    BitConverter.ToString((byte[])m.Body, 0);
                }
            }
            if (m.Body.GetType() == typeof(XmlElement))
            {
                XmlElement el = (XmlElement)m.Body;
                if (el.Name == "string")
                    Data = el.InnerText;
                else
                    Data = el.OuterXml;
            }

我想指出我正在設置消息的格式化程序,這是讓正文在隊列中自動“裝箱”和“拆箱”的第一步。

在 Windows XP 中,m.Body.GetType() == byte[],如預期的那樣。 但是,在 Windows 7 中,m.Body.GetType() == XmlElement,即包裝器 XML。 它不再“拆箱”消息。

我們需要做一些不同的事情嗎? 正如您在接收 function 的末尾看到的那樣,我們已經解決了一次發送字符串的問題,但我想找到一個真正的答案來解釋為什么此代碼在 Windows 7 上的行為不同。

如果要發送字節數組,請使用 Message.BodyStream 屬性:

System.Messaging.MessageQueue queue = new MessageQueue(queueFormatName, false, true, QueueAccessMode.Send);

System.Messaging.Message msg = new System.Messaging.Message();
msg.BodyStream = new MemoryStream(buffer);

queue.Send(msg, MessageQueueTransactionType.Single);

使用Message.BodyStream屬性發送和接收消息,看看下面的代碼你可以使用它發送和接收byte[]

public void SendMessage()
{
    MessageQueue myQueue = new MessageQueue(".\\QUEUE");
    byte[] msg = new byte[2];
    msg[0] = 29;               
    // Send the array to the queue.
    Message msg1 = new Message();
    msg1.BodyStream = new MemoryStream(msg);
    messageQueue.Send(msg1);                
}

public void ReceiveMessage()
{
    MessageQueue myQueue = new MessageQueue(".\\QUEUE");               
    Message myMessage =myQueue.Receive();
    byte[] msg = ReadFully(myMessage.BodyStream);
}

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM