How to read msmq messages (me, not the pc)

前端 未结 6 1792
梦如初夏
梦如初夏 2021-02-20 02:25

I want to look inside my queues, the msm console snapin has this property dialog, but it is very difficult to read and the messages which are important to me are encoded and loo

相关标签:
6条回答
  • 2021-02-20 02:52

    I found these two methods while searching for an answer to this question and they actually worked perfectly.

        public System.Xml.XmlDocument ConvertToXMLDoc(System.Messaging.Message msg)
        {
            byte[] buffer = new byte[msg.BodyStream.Length];
            msg.BodyStream.Read(buffer, 0, (int)msg.BodyStream.Length);
            int envelopeStart = FindEnvolopeStart(buffer);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer, envelopeStart, buffer.Length - envelopeStart);
            System.ServiceModel.Channels.BinaryMessageEncodingBindingElement elm = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();
            System.ServiceModel.Channels.Message msg1 = elm.CreateMessageEncoderFactory().Encoder.ReadMessage(stream, Int32.MaxValue);
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(msg1.GetReaderAtBodyContents());
            msg.BodyStream.Position = 0;
            return doc;
        }
    
        private int FindEnvolopeStart(byte[] stream)
        {
            int i = 0;
            byte prevByte = stream[i];
            byte curByte = (byte)0;
            for (i = 0; i < stream.Length; i++)            
            {
                curByte = stream[i];
                if (curByte == (byte)0x02 &&
                prevByte == (byte)0x56)
                    break;
                prevByte = curByte;
            }
            return i - 1;
        }
    

    Simply call the ConvertToXmlDoc function, providing the message from the message queue and you'll get an XmlDocument back. I am lazy, so I just drop the innerXml into a file so I can read it.

        MessageQueue queue = new MessageQueue(queueName);
        var msg = queue.Receive();
        var doc = ConvertToXMLDoc(msg);                
        using (var sw = new StreamWriter(@"C:\message.txt")))
               sw.Write(doc.InnerXml);
    

    No application to buy and you get your data back in code so you can mess around with it.

    PS: Credit where credit is due. The snippet came from http://social.msdn.microsoft.com/forums/en-US/wcf/thread/c03d80cd-492c-4ece-8890-6a35b12352e0 , which also links to a more detailed discussion of MSMQ's encoding format.

    0 讨论(0)
  • 2021-02-20 02:52

    You can use Service Bus MQ Manager, its a free open-source tool I wrote for viewing messages in MSMQ, it supports coloring and formatting of XML and JSON messages.

    http://blog.halan.se/page/Service-Bus-MQ-Manager.aspx

    0 讨论(0)
  • 2021-02-20 02:53

    Try this:

    string QueueName = @".\private$\publishingQueue"; 
    
    //note, you cannot use method exists on remote queues
    
    if (MessageQueue.Exists(QueueName))
    { 
        var queue = new MessageQueue(queueInfo.QueueName)
        {
            MessageReadPropertyFilter = new MessagePropertyFilter
            {
                ArrivedTime = true,
                Body = true
            }
        };
    
        var messages = queue.GetAllMessages();
        var m = messages[0];
        m.Formatter = new System.Messaging.XmlMessageFormatter(new String[] {});
    
        StreamReader sr = new StreamReader(m.BodyStream);
    
        string ms = "";
        string line;
    
        while (sr.Peek() >= 0) 
        {
            ms += sr.ReadLine();
        }
    
        //ms now contains the message      
    }
    
    0 讨论(0)
  • 2021-02-20 03:00

    You could also check out MSMQ Studio from https://msmq-studio.com

    0 讨论(0)
  • 2021-02-20 03:00

    This is about the best tool I've found: http://www.cogin.com/msmq/QueueExplorer/QueueExplorer2.2.php

    0 讨论(0)
  • 2021-02-20 03:03

    If you just have some hexadecimal data that can easily be converted to ASCII and back, then I suggest a text editor that lets you do just that. UltraEdit has a "view hex" function that works to convert both to and from hexadecimal view. You might also try Notepad++ but I don't know if it has that feature.

    0 讨论(0)
提交回复
热议问题