消息队列具有不同的消息类型 [英] Message Queues with different message types
问题描述
我在调查微软消息队列做进程间跨网络的消息。但是,当我收到一个消息,我不知道的先验的对象是什么我得到的类型,所以代码
I'm investigating Microsoft Message Queues for doing inter-process cross-network messaging. But when I receive a message, I don't know a priori what type of object I'm getting, so the code
queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Wibble) });
不能适用的前的我得到的消息,因为我不知道如果它是一个维布勒。所以,我如何收到不同的消息?
can't be applied before I get the message because I don't know if it's a Wibble. So how do I receive different message types?
推荐答案
您可能会考虑不存储在MSMQ消息的对象,而是把一个参考它的持久位置,如果你能。 MSMQ对消息队列有限的空间,所以较小的消息是最好的。
You might consider not storing your object in the MSMQ message, but instead putting a reference to it's persistent location if you can. MSMQ has finite space on the message queues, so smaller messages are best.
如果你不能做到这一点,你可以序列化对象消息直接的BodyStream,使用无论串行你喜欢。然后在消息标签存储的类型名称为好,可能是最好的。
If you can't do that, you can serialize your object to the messages BodyStream directly, using whatever serializer you like. Then store the type name as well, probably best in the message Label.
东西非常相似,这(在这里抓出来,这台电脑上没有IDE)把它在和的出路在analagous动作:
Something very similar to this (scratched it out here, no IDE on this computer) to put it in, and the analagous action on the way out:
public void FormatObject(object toFormat, Message message)
{
var serializer = new XmlSerializer(toFormat.GetType());
var stream = new MemoryStream();
serializer.Serialize(toFormat, stream);
//don't dispose the stream
message.BodyStream = stream;
message.Label = toFormat.GetType().AssemblyQualifiedName;
}
这篇关于消息队列具有不同的消息类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!