【问题标题】:How to read an MSMQ message object from a queue as a string of the message body如何从队列中读取 MSMQ 消息对象作为消息正文的字符串
【发布时间】:2011-06-16 03:50:34
【问题描述】:

是否可以直接以字符串形式从队列中读取消息(与从计算机管理 MSC 管理单元查看 MSMQ 消息时看到的消息正文相同),以便我自己将其保存到数据库中?我希望能够存储它以供以后重播而不会丢失任何数据,我担心如果反序列化/序列化它可能会丢失一些东西。

【问题讨论】:

  • 您是否只对保存邮件正文而没有其他邮件属性感兴趣?
  • 只是为了确定:您想拦截队列中的每条消息并获取消息正文的副本,但保持消息不变?

标签: msmq


【解决方案1】:

试试这个..

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      
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 2018-02-20
    • 2010-12-10
    • 2013-01-27
    • 2011-01-30
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多