How to correlate request/response messages by using System.Messaging.

Article translations Article translations
Article ID: 555298 - View products that this article applies to.
Author: Carlos Walzer MVP
Expand all | Collapse all

SUMMARY

This article describes how to correlate request/response messages by using System.Messaging.

SYMPTOMS

The following article explains how to correlate request/response messages by using System.Messaging.
It applies to a typical scenario where you can find one or more client applications that require information and processing from a central server application. Both applications client and server, exchange response and request information by sending messages to msmq queues.
The clients running on different machines send request messages to the
server, which has to send back response messages.
Two questions arise here:
If the server receives several messages from different clients, how does it know which of the clients' queues it needs to send the response to?
If the client sends several messages to the server, how does it know which of the request messages each response message corresponds to?

RESOLUTION

Any client application sends a request in a message to the server application and saves the Message.Id in memory. The server application receives the message, processes the request and sends a response message, setting its Message.CorrelationId property with the received Message.Id.  Then the client application looks up for the message response based in the request Message.Id.
The other topic that has to be covered by the server application is to attend to many clients queuing theirs requests. The client sets the Message.ResponseQueue property of the request message and sends it to the server. This property contains information about the queue where the client wants to receive the response message. The server receives the request message with the corresponding Message.ResponseQueue property, so it already has the specifications to address the reply.

MORE INFORMATION

The following example shows two applications, a client and a server, correlating messages and delivering response message to specified response queues.
 
Assumptions:
Both client and server's queues already exist. The server application runs in a computer called "ServerPC" with a public queue called "ServiceA" from where it receives the request messages. The client application resides in another computer with a private queue called "ServiceAResponses" where response messages will be arriving.
Note: For better performance and failure avoiding receive message only from local Queues.
 
Example:
The logic sequence of the following example should be
1. Client.SendRequest()
2. Server.ProcessRequest()
3. Client.GetResponse()
 
/// <summary>
/// Class of the Server app.
/// </summary>
class Server
{
 static void ProcessRequest()
 {
  //Receive the message and send the response to the response queue
  q.Formatter = new XmlMessageFormatter (new String[]{"System.String"});
  Message requestMsg = q.Receive(new TimeSpan(0, 0, 2));
  
  //Process Request Message
  // Set request information in requestMsg.Body
  //.............
  //Get the response queue from the receiveMsg
  Queue response = requestMsg.ResponseQueue;
  //Set the correlationId of the message to the Id of the original message
  receiveMsg.CorrelationId = requestMsg.Id;
  //Send the response message to the response queue
  response.Send(requestMsg);
 }
}
/// <summary>
/// Class of the Client app.
/// </summary>
public class Client
{
 private string _corrId;
 private Queue _response;
 public void Client()
 {
  //Create an instance of the ResponseQueue
  _response = new MessageQueue(@".\private$\ServiceAResponses");
 }
 public void SendRequest()
 {
  //Create an instance of the MessageQueue
  MessageQueue q = new MessageQueue (@"ServerPC\ServiceA");
  String message = "Request Message";
  //Create an instance of the Message
  System.Messaging.Message requestMsg = new System.Messaging.Message (message);
  //Assign the ResponseQueue property
  requestMsg.ResponseQueue = _response;
  //Send the message
  q.Send(requestMsg);
  //Get the Id of the sent message.
  _corrId = requestMsg.Id;
 }
 public void GetResponse()
 {
      //Receive the Response message by mapping
  //the CorrrelationId to the Id of the
  //Original message
  Message responseMsg = _response.ReceiveByCorrelationId(corrId, new TimeSpan(0, 0, 30));
   responseMessage.Formatter = (new XmlMessageFormatter(new Type[]{typeof(System.String)}));
  //Process Response Message
  // Take response information from responseMsg.Body
  //.............
 }
 
Important Note: ReceiveByCorrelationId uses sequence search to find a message in a queue. Use it with carefulness; it might be inefficient when a high number of messages reside in a queue.

Properties

Article ID: 555298 - Last Review: April 23, 2005 - Revision: 1.0
APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbpubmvp kbpubtypecca kbhowto KB555298
COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com