我正在用C#中的消息队列写消息,如下所示:
ObjectMsg objMsg = new ObjMsg(1,"ascii",20090807);
Message m = new Message();
m.Formatter = new BinaryMessageFormatter();
m.body = objMsg;
queue.Send(m);
我正在尝试阅读如下消息:
Message m = new Message()
m.Formatter = new BinaryMessageFormatter();
MessageQueue mq = new MessageQueue("./pqueue");
m = mq.Recieve();
ObjMsg msg = (ObjMsg )m.Body;
但是我收到一条错误消息,上面写着:“找不到能够读取此消息的格式化程序。”
答案 0 :(得分:1)
尝试阅读消息的正文流:
Message m = new Message()
m.Formatter = new BinaryMessageFormatter();
MessageQueue mq = new MessageQueue("./pqueue");
m = mq.Recieve();
StreamReader sr = new StreamReader(m.BodyStream);
string messageBody = "";
while (sr.Peek() >= 0)
{
messageBody += sr.ReadLine();
}
答案 1 :(得分:0)
您也可以拨打StreamReader.ReadToEnd()
一次,而不是不断追加字符串。根据我的经验,这通常会更快。
System.Messaging.Message[] messages = messageQueue.GetAllMessages();
foreach (System.Messaging.Message message in messages)
{
System.IO.StreamReader sr = new System.IO.StreamReader(message.BodyStream);
Console.WriteLine(sr.ReadToEnd());
}