MSMQ + C#,接收带有编码byte []主体的消息在Windows 7与Windows XP上的行为有所不同

时间:2011-05-02 20:40:51

标签: c# .net windows-7 msmq

我有一个应用程序,它在MSMQ消息队列中显示消息的内容。 Windows 7上的MSMQ存在问题,但不保留邮件正文中数据的真实对象类型。在这个例子中,我发送一个byte [],然后当我收到它时,它不再是一个字节数组,而是包装的XML容器文档。在Windows XP中,我从未遇到过此问题,并且Message.Body属性始终正确设置为byte []。

以下是压缩代码:

    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);
        }
    }

以下是测试消息的消息内容。它最终成为包装编码二进制数据的XML文档:     <?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。它不再“取消装箱”消息。

我们需要采取不同的做法吗?我们已经解决了一次发送字符串的问题,正如您在接收函数结束时所看到的那样,但我想找到一个真正的答案,解释为什么此代码在Windows 7上的行为不同。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

使用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();
    }
}