Search code examples
c#message-queueremoteobject

Retrieving object sent through a Message Queue


I am building a Queue system and I have been able to send an object to a public queue located on another server. What I cannot figure out is how to rebuild the object on the receiver side (I have the its definition on both ends).

Any ideas?


Solution

  • Have a look at the following MSDN example: http://msdn.microsoft.com/en-us/library/y918yfy2(v=vs.110).aspx

    Basically, calling queue.Send(object) serializes the object with the default XmlMessageFormatter. So you'll have to deserialize the message using the same serializer, and cast the result of the received Message.Body to the good type:

    // Connect to the a queue on the local computer.
    MessageQueue myQueue = new MessageQueue(".\\myQueue");
    
    // Set the formatter to indicate body contains an Order.
    myQueue.Formatter = new XmlMessageFormatter(new Type[] {typeof(MyProject.Order)});
    
    // Receive and format the message. 
    Message myMessage = myQueue.Receive(); 
    Order myOrder = (Order)myMessage.Body;