简体   繁体   English

更改MSMQ正文的格式

[英]Change format for MSMQ body

I have an object which I would like to store in XML format in an MSMQ queue. 我有一个要以XML格式存储在MSMQ队列中的对象。 What I expect it to appear in the queue body is this: 我希望它出现在队列主体中是这样的:

<?xml version="1.0"?>
<Object>
    <Text>Hello World!</Text>
    <Number>5</Number>
</Object>

However, when I store the object, I would see a diferent result in the body: 但是,当我存储对象时,我会在主体中看到不同的结果:

3C 3F 78 6D 6C 20 76 65 <?xml ve
72 73 69 6F 6E 3D 22 31 rsion="1
2E 30 22 3F 3E 0D 0A 3C .0"?>..<
4F 62 6A 65 63 74 3E 0D Object>.
0A 09 3C 54 65 78 74 3E .<Text>H
48 65 6C 6C 6F 20 57 6F ello Wor
72 6C 64 21 3C 2F 54 65 ld!</tex
78 74 3E 0D 0A 09 3C 4E t>..<Num 
75 6D 62 65 72 3E 35 3C ber>5</N
2F 4E 75 6D 62 65 72 3E umber>..
0D 0A 3C 2F 4F 62 6A 65 </Object
63 74 3E                >

I'm not sure what the actual format used here is, so I can't workout how I would alter it. 我不确定这里使用的实际格式是什么,所以我无法锻炼如何更改它。 I have tried adding messageQueue.Formatter = new BinaryMessageFormatter(); 我尝试添加messageQueue.Formatter = new BinaryMessageFormatter(); and messageQueue.Formatter = new XmlMessageFormatter(); messageQueue.Formatter = new XmlMessageFormatter(); to the MessageQueue object, but I get exactly the same result. 到MessageQueue对象,但得到完全相同的结果。

My code for sending the object is: 我发送对象的代码是:

// This part of the code of creating the TestObject isn't what I'm using in development, 
// this is just for showing you that I'm using an object.
TestObject item = new TestObject();
item.Text = "Hello World!";
item.Number = 5;

MessageQueue messageQueue;
Message message;

messageQueue = new MessageQueue(@".\Private$\myqueue");
message = new Message(item);

messageQueue.Send(message);

How would I alter my code to make sure it sends and stores in an XML format? 如何更改代码以确保其以XML格式发送和存储?

Any help would be appreciated. 任何帮助,将不胜感激。

The hex dump above almost appears to be the same result you would get if you right-clicked properties in " Computer Management / Message Queuing / Private Queues / myqueue / Queue messages " on a message and selected the "Body" tab. 如果右键单击消息上的计算机管理/消息队列/专用队列/ myqueue /队列消息 ”中的属性,然后选择“正文”选项卡,则上面的十六进制转储几乎看起来与您得到的结果相同。 When you do that, it's possible for the viewer to present you with either a string dump or hex dump. 当您执行此操作时,查看者可以向您展示字符串转储或十六进制转储。 Typically a large size presents you with a hex dump. 通常,较大的尺寸会为您带来十六进制转储。

To have your objects serialized as XML, just set the queue's MessageQueue.Formatter or message's Message.Formatter (for just a particular message) to an XmlMessageFormatter , but include the types you'll need. 要将对象序列化为XML,只需将队列的MessageQueue.Formatter或消息的Message.Formatter (仅用于特定消息)设置为XmlMessageFormatter ,但要包含所需的类型。 Then the object will become a serialized XML message. 然后,该对象将成为序列化的XML消息。 Included below is the code posted with some additions. 下面包括附带一些附加代码的代码。

// This part of the code of creating the TestObject isn't what I'm using in development, 
// this is just for showing you that I'm using an object.
TestObject item = new TestObject();
item.Text = "Hello World!";
item.Number = 5;

MessageQueue messageQueue;
Message message;
XmlMessageFormatter format = new XmlMessageFormatter(
    new Type[] {
        typeof(string),
        typeof(TestObject)}
);

messageQueue = new MessageQueue(@".\Private$\myqueue");
message = new Message(item);
message.Formatter = format;

messageQueue.Send(message);

// ...

message = messageQueue.Receive();
Type messageType = message.Body.GetType();
if (messageType == typeof(string))
{
    string newString = (string)message.Body;
}
else if (messageType == typeof(TestObject))
{
    TestObject receiveItem = (TestObject)message.Body;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM