Search code examples
c#message-queuemsmq

InvalidOperationException when trying to access Body from Message


I try to access the messages of the windows message queue:

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

I receive an InvalidOperationException when trying to access message.Body (And on nearly every other property other than Id - fields).


Solution

  • Thanks to @Soner Gönül I was able to solve my problem. This is the solution:

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