I need to communicate with a legacy application from my C# app via the Windows Message Queue.
我需要通过Windows消息队列与c#应用程序中的遗留应用程序进行通信。
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!
遗留应用程序希望在特定的私有队列中使用纯字符串消息,但我似乎无法停止System.Messaging。用XML包装我的消息的MessageQueue !
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序列化,并以以下形式出现在队列中:
<?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.
我不能修改遗留应用程序的行为,因此需要停止System.Messaging。将消息格式化为XML的MessageQueue。
Can anyone help?
谁能帮忙吗?
2 个解决方案
#1
3
You can create your own formatter (it is a class that implements IMessageFormatter and assign it to the Formatter property of the Message
您可以创建自己的formatter(它是一个实现IMessageFormatter的类,并将其分配给消息的formatter属性
Here is a link to MSDN to the Message.Formatter property.
这里是一条消息的MSDN链接。格式化程序属性。
I have not tried this but you should be able to write your message using the BodyStream, I believe this will bypass the formatter.
我没有尝试过,但是您应该能够使用BodyStream来编写您的消息,我相信这将绕过格式化程序。
#2
3
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.
使用ActiveXMessageFormatter将给您想要的结果。我们还遇到了同样的问题,即只想将字符串传递给队列,并让侦听器进程作为字符串在主体中读取。ActiveXMessageFormatter用于序列化/反序列化原始数据类型,不会像默认XmlMessageFormatter那样在输入中放置XML包装器。
mq.Formatter = new ActiveXMessageFormatter();
Here is another link describing the 3 different formatters as well.
这是另一个描述3种不同形式的链接。
#1
3
You can create your own formatter (it is a class that implements IMessageFormatter and assign it to the Formatter property of the Message
您可以创建自己的formatter(它是一个实现IMessageFormatter的类,并将其分配给消息的formatter属性
Here is a link to MSDN to the Message.Formatter property.
这里是一条消息的MSDN链接。格式化程序属性。
I have not tried this but you should be able to write your message using the BodyStream, I believe this will bypass the formatter.
我没有尝试过,但是您应该能够使用BodyStream来编写您的消息,我相信这将绕过格式化程序。
#2
3
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.
使用ActiveXMessageFormatter将给您想要的结果。我们还遇到了同样的问题,即只想将字符串传递给队列,并让侦听器进程作为字符串在主体中读取。ActiveXMessageFormatter用于序列化/反序列化原始数据类型,不会像默认XmlMessageFormatter那样在输入中放置XML包装器。
mq.Formatter = new ActiveXMessageFormatter();
Here is another link describing the 3 different formatters as well.
这是另一个描述3种不同形式的链接。