Sometime back I had blogged about how to send messages to remote private queues. There were a few queries on how to read from remote queue as well.
To me it should have worked by using the MessageQueue.Receive API instead of .Send. I decided to give this a try anyway and it indeed is as trivial as that. However a few things to take care while trying to get this working.
1. Check the documentation to see where all the API works. The API may have limitations in working in workgroup mode if you are not using the direct format name syntax.
2. The other very important aspect is setting the formatter prior to receiving the message. You need to set this appropriately to binary or XML and specific type to ensure successful read.
To show a complete example, find below code that posts to the remote queue (transactional as well as non-transactional) and also reads from them. To keep things simple, I have used a simple string message here. However you can definitely use more complex types and custom types for sending and receiving (as long as they match both ways).
private void btnPostToQueue_Click(object sender, EventArgs e)
{
//post to regular non-transactional remote queue using OS name
MessageQueue rmQ =new MessageQueue("FormatName:Direct=OS:punhjw30076\\private$\\remote");
rmQ.Send("sent to regular queue - Atul");
//post to transactional remote queue using IP address
MessageQueue rmTxnQ =new MessageQueue("FormatName:Direct=TCP:100.100.100.12\\private$\\remoteTxn");
rmTxnQ.Send("sent to transacted queue - Atul",MessageQueueTransactionType.Single);
}
private void btnGetFromQueue_Click(object sender, EventArgs e)
{
//retrieve from regular non-transactional remote queue using OS name
MessageQueue rmQ =new MessageQueue("FormatName:Direct=OS:punhjw30076\\private$\\remote");
rmQ.Formatter = newXmlMessageFormatter(newType[] { typeof(string) });
System.Messaging.Message msg = rmQ.Receive();
MessageBox.Show(msg.Body.ToString());
//retrieve from transactional remote queue using IP address
MessageQueue rmTxnQ =new MessageQueue("FormatName:Direct=TCP:100.100.100.12\\private$\\remoteTxn");
rmTxnQ.Formatter = newXmlMessageFormatter(newType[] { typeof(string) });
System.Messaging.Message msgTxn = rmTxnQ.Receive(MessageQueueTransactionType.Single);
MessageBox.Show(msgTxn.Body.ToString());
}