简体   繁体   中英

How can I send messages of different types through MSMQ?

I have messages of different types that I need to send to a client via MSMQ. I'm not using WCF as I want a solution that I code myself. How can I differentiate the datatype of the message body when receiving the message on the client as I'd ideally want to set up the IFormatter based on the type of the message body as I interrogate the messages. Thank you.

If you have control over the message format being sent, you could wrap each message body in a fixed container with a 'type' attribute. You could then deserialize the container and determine how to handle the deserialisation of the actual contained message based on the 'type' attribute.

eg

<dataContainer type="XYZ">
 <!-- put your actual message content here inside a CDATA section -->
</dataContainer>

Typically if you have multiple types of messages, you use multiple queues. This avoids any potential issues later on of being able to de-queue only specific types of messages, and of any potential confusion over the type of message being processed.

If you've already got your infrastructure working with one queue, adding another should be minimal effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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