【问题标题】:InvalidOperationException when trying to access Body from Message尝试从消息访问正文时出现 InvalidOperationException
【发布时间】:2015-02-14 22:44:32
【问题描述】:

我尝试访问windows消息队列的消息:

var activeQueue = new MessageQueue("\\myhost\\private$\\just.a.queue", QueueAccessMode.Receive);
foreach(message in _activeQueue.GetAllMessages().ToList()) {
   Console.WriteLine(message.Body);
}

我在尝试访问 message.Body 时收到 InvalidOperationException(以及几乎所有其他属性,除了 Id - 字段)。

【问题讨论】:

  • 什么是堆栈跟踪?
  • System.InvalidOperationException: Ein Formatierungsprogramm, das diese Meldung lesen kann, wurde nicht gefunden。 bei System.Messaging.Message.get_Body() bei QueueTest.Program.Main(String[] args) in c:\QueueTest\Program.cs:Zeile 25. 翻译:“找不到可以读取此消息的格式化程序”或类似的事情你的问题引导我走向正确的方向。看来我必须提供格式化程序

标签: c# message-queue msmq


【解决方案1】:

感谢@Soner Gönül,我能够解决我的问题。这是解决方案:

message.Formatter = new ActiveXMessageFormatter();
var reader = new StreamReader(message.BodyStream);
var msgBody = reader.ReadToEnd();
Console.WriteLine(msgBody)

【讨论】:

  • 感谢您抽出宝贵时间亲自回答问题。
【解决方案2】:

附带说明一下,如何使用格式化程序来解码消息有两种可能性:

Queue.Formatter = new MessageFormatter(); // Set formatter on queue
Msg = Queue.Receive();
Body = Msg.Body;

Msg = Queue.Receive();
Msg.Formatter = new MessageFormatter(); // Set formatter on msg
Body = Msg.Body;

也可以组合:

Queue.Formatter = new MessageFormatter();
Msg = Queue.Receive();
if(Msg.Label.Contains('Other')) Msg.Formatter = new OtherMessageFormatter();
Body = Msg.Body;

但以下将失败:

Msg = Queue.Receive();
Queue.Formatter = new MessageFormatter();

因为当队列接收到消息时,消息的格式化器会被初始化为队列当前的格式化器,所以如果队列的格式化器只在已经接收到第一条消息后才设置,它将不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2020-12-05
    • 2022-09-28
    相关资源
    最近更新 更多