Asynchronous Message Queuing
Using the .NET and COM+ Services

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft .NET SDK

The coolest thing is that MSMQ (and the COM+ Queued Components wrapper technology) comes with Windows 2000. You don't need SQL Server, or even the Windows Active Directory, to use it.

The .NET framework uses the System.Messaging namespace to leverage the Message Queuing Services provided by COM+.  

I will illustrate this by developing a Command-Line based Asynchronous Messaging Receiver and Server. The Server can send multiple messages which are queued in order and read by the client application.

Develop the Asynchronous Message Queuing Client and Server

Messages that are sent from different instances of the Server reach the Messaging Queue which queues them in an internally. These messages can at any time be retrieved from the queue asynchronously by the Client.

1. Develop The Server Application : MSMQServer.EXE
.

MSMQServer.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
/**
* File: MSMQServer.cs
* Article: Asynchronous Message Queuing using the .NET and COM+ Services
* Copyright (c) 2000, Gopalan Suresh Raj. All Rights Reserved.
* Compile with: csc /debug+ /r:system.messaging.dll MSMQServer.cs
* Run As: MSMQServer <Queue Name> <Message to Send>
*         eg., MSMQServer testQueue "This is a test"
*/


using System;
using System.Messaging;

namespace com.icommware {

    public class MSMQServer {

     public static void Main (String[] args) {
    
      // Get the name of this Message Sender application
      string applicationName = Environment.GetCommandLineArgs ()[0];
      // Test that the number of arguments passed in are ok
      if (args.Length != 2) {
       Console.WriteLine ("Usage: "+
                          applicationName+
                          " <Queue Name> <Message to Send>");
       return;
      }

      string messageQueuePath = ".\\"+args [0];
      // If such a Message Queue does not exist
      if (MessageQueue.Exists (messageQueuePath) == false) {
       // Create a Message Queue Path
       MessageQueue.Create (messageQueuePath);
      }
      // Create a MessageQueue Object for that Queue
      MessageQueue messageQueue = new MessageQueue (messageQueuePath);
      // Send the Message to this Queue
      messageQueue.Send (args [1]);
     }
    }
}

2. Develop the Client Application : MSMQClient.EXE
.

MSMQClient.cs
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
/**
* File: MSMQClient.cs
* Article: Asynchronous Message Queuing using the .NET and COM+ Services
* Copyright (c) 2000, Gopalan Suresh Raj. All Rights Reserved.
* Compile with: csc /debug+ /r:system.messaging.dll MSMQClient.cs
* Run As: MSMQClient <Queue Name>
*         eg., MSMQClient testQueue
*/


using System;
using System.Messaging;
using System.Threading;

namespace com.icommware {

    public class MSMQClient {

     public static void Main (String[] args) {
    
      // Get the name of this Message Sender application
      string applicationName = Environment.GetCommandLineArgs ()[0];
      // Test that the number of arguments passed in are ok
      if (args.Length != 1) {
       Console.WriteLine ("Usage: "+
                          applicationName+
                          " <Queue Name>");
       return;
      }

      string messageQueuePath = ".\\"+args [0];
      // If such a Message Queue does not exist
      if (MessageQueue.Exists (messageQueuePath) == false) {
       // Give out an Error Message to that effect and Exit
       Console.WriteLine ("The Specified Queue "+
                          messageQueuePath+
                          " does not exist...");
       return;                  
      }
      // Create a MessageQueue Object for that Queue
      MessageQueue messageQueue = new MessageQueue (messageQueuePath);
      // Setup a Message Handler for this Queue
      messageQueue.AddOnReceiveCompleted (new ReceiveCompletedEventHandler (Arrived));
      // Start looking for messages
      messageQueue.BeginReceive ();

      // Block and stay alive on the Console
      Console.WriteLine ("Press enter to stop the program...");
      Console.ReadLine ();
     } // end main
    
     // The Message Handler for receiveing Messages Asynchronously
     public static void Arrived (Object source, ReceiveAsyncEventArgs asyncReceive) {
    
      // Get a handle to the Message Queue
      MessageQueue messageQueue = (MessageQueue) source;
      // Start Receiving Messages
      Message message = messageQueue.EndReceive (asyncReceive.AsyncResult);
      // Display the Received Message
      Console.WriteLine ("The Message Received Reads :" + (string)message.Body);
      // Start Looking for Messages again
      messageQueue.BeginReceive ();
     } // end Arrived
    } // end MSMQClient class
} // end com.icommware namespace

3. Compile the Server and the Client Applications

MS-DOS Command Prompt
H:\csharpForays\MSMQ>csc /debug+ /r:system.messaging.dll MSMQServer.cs
Microsoft (R) C# Compiler Version 7.00.8905 [NGWS runtime 2000.14.1812.10]
Copyright (C) Microsoft Corp 2000. All rights reserved.


H:\csharpForays\MSMQ>
H:\csharpForays\MSMQ>
H:\csharpForays\MSMQ>
csc /debug+ /r:system.messaging.dll MSMQClient.cs
Microsoft (R) C# Compiler Version 7.00.8905 [NGWS runtime 2000.14.1812.10]
Copyright (C) Microsoft Corp 2000. All rights reserved.


H:\csharpForays\MSMQ>
H:\csharpForays\MSMQ>

4. Run One Instance of the Client (MSMQClient.EXE) and Multiple Instances of the Server (MSMQServer.EXE)

Remember to startup the Message Queuing Component Service and make sure it's running before executing either the Client or the Server applications.

i.e., Administrative Tools->Component Services->Services (Local)->Message Queuing

MS-DOS Command Prompt 
H:\csharpForays\MSMQ>
H:\csharpForays\MSMQ>
MSMQServer testQueue "This is a test"

H:\csharpForays\MSMQ>
start MSMQClient testQueue

H:\csharpForays\MSMQ>
MSMQServer testQueue "Hello Athul..."

H:\csharpForays\MSMQ>
MSMQServer testQueue "This is Gopalan..."

H:\csharpForays\MSMQ>
MSMQServer testQueue "How are you doing?"

H:\csharpForays\MSMQ>

The Client Window that opened up as a result of executing the "start" DOS Command appears on the screen as follows.

H:\csharpForays\MSMQ\MSMQClient.exe
Press enter to stop the program...
The Message Received Reads :This is a test
The Message Received Reads :Hello Athul...
The Message Received Reads :This is Gopalan...
The Message Received Reads :How are you doing?


Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

click here to go to
My Microsoft Message Queue (MSMQ) Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since October 26, 2000.

Last Updated : Oct 26, '00

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.