Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message Queue Error: cannot find a formatter capable of reading message

I'm writing messages to a Message Queue in C# as follows:

queue.Send(new Message("message"));

I'm trying to read the messages as follows:

Messages messages = queue.GetAllMessages();
foreach(Message m in messages)
{
  String message = m.Body;
  //do something with string
}

However I'm getting an error message which says: "Cannot find a formatter capable of reading this message."

What am I doing wrong?

like image 734
macleojw Avatar asked Mar 17 '09 17:03

macleojw


4 Answers

I solved the problem by adding a formatter to each message. Adding a formatter to the queue didn't work.

Messages messages = queue.GetAllMessages();
foreach(Message m in messages)
{
  m.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
  String message = m.Body;

  //do something with string
}
like image 68
macleojw Avatar answered Nov 02 '22 10:11

macleojw


Or you can use

 message.Formatter =
     new System.Messaging.XmlMessageFormatter(new Type[1] { typeof(string) });
like image 33
prime23 Avatar answered Nov 02 '22 09:11

prime23


you could try reading the bodystream of the message instead of the body, like this:

StreamReader sr = new StreamReader(m.BodyStream);    
string messageBody = "";    
while (sr.Peek() >= 0) 
{
    messageBody += sr.ReadLine();
}
like image 6
felbus Avatar answered Nov 02 '22 10:11

felbus


Message recoverableMessage = new Message();
recoverableMessage.Body = "Sample Recoverable Message";

recoverableMessage.Formatter = new XmlMessageFormatter(new String[] {"System.String,mscorlib" });

MessageQueue myQueue = new MessageQueue(@".\private$\teste");

Queue must be set Formatter too.

myQueue.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
like image 4
Mr Milk Avatar answered Nov 02 '22 10:11

Mr Milk