2013-12-11 181 views
0

我已經創建了一個簡單的程序來在私人消息隊列中記錄「hello world」消息。收到MSMQ消息

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

但是當我嘗試在一個文本框來顯示毫秒這是我得到

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

回答

1

您必須使用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; 

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