接收MSMQ消息

时间:2013-12-11 17:00:08

标签: c# message msmq

我创建了一个简单的程序来在私人消息队列中记录“hello world”消息。

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

以下是我试图收到它的方式

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

但是当我尝试在文本框中显示ms时,这就是我得到的

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

1 个答案:

答案 0 :(得分:1)

您必须使用Body属性

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

BodyStream属性返回一个Stream,其中包含消息正文中包含的序列化信息。