views:

361

answers:

1

I need to communicate with a legacy application from my C# app via the Windows Message Queue.

The legacy application expects plain string messages in a particular private queue, but I can't seem to stop the System.Messaging.MessageQueue from wrapping my message in XML!

The code I'm testing is very simple:

MessageQueue myQueue = new MessageQueue(@".\Private$\tolegacy");
Message msg = new Message("My Test String");
myQueue.Send(msg);

The trouble is that the message is being XML serialized and appears in the queue as:

<?xml version="1.0"?><string>My Test String</string>

I can't modify the behaviour of the legacy application, so I need to stop the System.Messaging.MessageQueue from formatting my message as XML.

Can anyone help?

+2  A: 

You can create your own formatter (it is a class that implements IMessageFormatter and assign it to the Formatter property of the Message

Here is a link to MSDN to the Message.Formatter property.

I have not tried this but you should be able to write your message using the BodyStream, I believe this will bypass the formatter.

JD
Writing directly to the BodyStream worked, thanks. It looked like the easiest change so I tried it first :)
Damovisa
I've written several myself and they work really well. The only thing you have to worry about is the sender and reciever understanding the body type.
Joe Caffeine
We are having similar issue. We can read/write queue and get message back fine. But when vendor reads queue, he sees the extra "stuff" wrapped around our message. How can we just write a simple string with no wrapping at all? Even BinaryFormatter added a bunch of bytes at the beginning of the string.
NealWalters
Answer was - remove all formatters - MemoryStream myStream = new MemoryStream(System.Text.encoding.ASCII.GetBytes(myString);msg.BodyStream = myStream; Q.Send(msg);
NealWalters