Search code examples
c#listmsmqsend

How to Send and Receive a LIST via MSMQ / C#


How can I send and receive a LIST object via MSMQ in C#?

Hard to find suggestions.

Detail examples greatly appreciated.


Solution

  • To send the list, you may just do:

    queue.Send(list);
    

    To receive the list, use this:

    queue.Formatter = new XmlMessageFormatter(new Type[]{typeof(List<T>)}); //Replace T with the type your list holds
    Message message = queue.Receive();
    List<T> list = (List<T>)message.Body;
    

    When the message is sent, the object is formatted using XML. So, when receiving, you must specify that the queue uses a XML formatter with whatever type of list you have. Then, after receiving, the message's body must be casted to the list of the proper type.