How do I stop my System.Messaging.MessageQueue from wrapping my strings in XML?
Asked Answered
U

2

6

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?

Unsound answered 2/6, 2009 at 0:5 Comment(0)
B
3

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.

Befuddle answered 2/6, 2009 at 0:21 Comment(5)
Writing directly to the BodyStream worked, thanks. It looked like the easiest change so I tried it first :)Unsound
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.Oaxaca
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.Final
Answer was - remove all formatters - MemoryStream myStream = new MemoryStream(System.Text.encoding.ASCII.GetBytes(myString); msg.BodyStream = myStream; Q.Send(msg);Final
To correct a few bugs in @NealWalters's above comment, it's using System.Text; then var memStream = new MemoryStream(Encoding.ASCII.GetBytes(myString)); then var msg = new Message { BodyStream = memStream }; then Q.Send(msg);Archuleta
C
4

Using the ActiveXMessageFormatter will give you the desired result. We had the same issue with just wanting to pass a string to a queue and have the listener process read in the body as a string. The ActiveXMessageFormatter is used for serializing/deserializing primitive data types and will not put an XML wrapper on your input as is the case with the default XmlMessageFormatter.

mq.Formatter = new ActiveXMessageFormatter();

Here is another link describing the 3 different formatters as well.

Collative answered 9/5, 2012 at 0:53 Comment(0)
B
3

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.

Befuddle answered 2/6, 2009 at 0:21 Comment(5)
Writing directly to the BodyStream worked, thanks. It looked like the easiest change so I tried it first :)Unsound
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.Oaxaca
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.Final
Answer was - remove all formatters - MemoryStream myStream = new MemoryStream(System.Text.encoding.ASCII.GetBytes(myString); msg.BodyStream = myStream; Q.Send(msg);Final
To correct a few bugs in @NealWalters's above comment, it's using System.Text; then var memStream = new MemoryStream(Encoding.ASCII.GetBytes(myString)); then var msg = new Message { BodyStream = memStream }; then Q.Send(msg);Archuleta

© 2022 - 2024 — McMap. All rights reserved.