简体   繁体   中英

Receiving an MSMQ message

I have created a simple program to log a "hello world" message in a private message queue.

 LogEntry lg = new LogEntry();
 lg.Message = "Hello World";
 Logger.Write(lg,"General");

Here is how I am trying to recieve it

string QueueName = ".\\Private$\\logline";
MessageQueue q1= q1 = new MessageQueue(QueueName);
System.Messaging.Message message=q1.Receive();
message.Formatter = new XmlMessageFormatter(new String[] { });
StreamReader sr = new StreamReader(message.BodyStream);
string ms = "";

while (sr.Peek() >= 0)
{
     ms += sr.ReadLine();
}

But When I try to display ms in a textbox this what i get

<?xml version="1.0"?>     <string>AAEAAAD/////AQAAAAAAAAAMAgAAAHJNaWNyb3NvZnQuUHJhY3RpY2VzLkVudGVycHJpc2VMaWJyYXJ5LkxvZ2dpbmcsIFZlcnNpb249NS4wLjQxNC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTMxYmYzODU2YWQzNjRlMzUMAwAAAElTeXN0ZW0sIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAA2TWljcm9zb2Z0LlByYWN0aWNlcy5FbnRlcnByaXNlTGlicmFyeS5Mb2dnaW5nLkxvZ0VudHJ5GwAAAAdtZXNzYWdlBXRpdGxlD2NhdGVnb3J5U3RyaW5ncwhwcmlvcml0eQdldmVudElkCmFjdGl2aXR5SWQRcmVsYXRlZEFjdGl2aXR5SWQIc2V2ZXJpdHkLbWFjaGluZU5hbWUJdGltZVN0YW1wDWVycm9yTWVzc2FnZXMSZXh0ZW5kZWRQcm9wZXJ0aWVzDWFwcERvbWFpbk5hbWUJcHJvY2Vzc0lkC3Byb2Nlc3NOYW1lCnRocmVhZE5hbWUNd2luMzJUaHJlYWRJZBR0aW1lU3RhbXBJbml0aWFsaXplZBhhcHBEb21haW5OYW1lSW5pdGlhbGl6ZWQWbWFjaGluZU5hbWVJbml0aWFsaXplZBRwcm9jZXNzSWRJbml0aWFsaXplZBZwcm9jZXNzTmFtZUluaXRpYWxpemVkGHdpbjMyVGhyZWFkSWRJbml0aWFsaXplZBV0aHJlYWROYW1lSW5pdGlhbGl6ZWQVYWN0aXZpdHlJZEluaXRpYWxpemVkIHVubWFuYWdlZENvZGVQZXJtaXNzaW9uQXZhaWxhYmxlK3VubWFuYWdlZENvZGVQZXJtaXNzaW9uQXZhaWxhYmxlSW5pdGlhbGl6ZWQBAQYAAAMDBAEAAwMBAQEBAQAAAAAAAAAAAAAICAtTeXN0ZW0uR3VpZG1TeXN0ZW0uTnVsbGFibGVgMVtbU3lzdGVtLkd1aWQsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dIVN5c3RlbS5EaWFnbm9zdGljcy5UcmFjZUV2Z</string>

You have to use the Body property

string QueueName = ".\\Private$\\logline";
MessageQueue myQueue = new MessageQueue(QueueName);
myQueue.Formatter = new BinaryMessageFormatter();
System.Messaging.Message myMessage = myQueue.Receive(); 
string myData = (string)myMessage.Body;

The BodyStream property returns a Stream that contains the serialized information included in the Body of the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM