如何停止我的系統。用XML包裝字符串的MessageQueue ?

[英]How do I stop my System.Messaging.MessageQueue from wrapping my strings in XML?


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種不同形式的鏈接。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/02/72994f6cd26a7474e0cd3773d19ac147.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com