Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Send Message to Microsoft Message Queue (MSMQ) – C# Example

1. Introduction to Message Queue

MSMQ stands for ‘Microsoft Message Queue’. MSMQ is a message storage area where one or more application contacts it to send or receive the messages. The message is a pack of information and it can be plain text or XML or Binary or another supported format. Now we will look the Fig.1 below:

Microsoft Message Queue (MSMQ)

Microsoft Message Queue (MSMQ)

Message Queues allows data exchange between one or more application and in the meantime, it won’t force the application being communicated should be in live connection. Application 1 can send a message and quit or do another task. Application 2 when free looks at the queue and catches the passed-in information.

MSMQ can maintain one or more queue. In the above picture, we can see three queues. We will develop a sample in this article, which will send and receive the information through the MSMQ as shown above. In our example, we will use only one queue, say Q1. However, one can create multiple queues based on the need.

2. Installing MSMQ Windows Component For Sending Messages

MSMQ is a windows component like IIS. So, first, we should make sure that the MSMQ component is installed on our machine. To do that, we will go to the services window by typing ‘services.msc’ in the run prompt and then make sure that the services as shown in Fin.2 are available and running in the machine:

Message Queuing Services

Message Queuing Services

If the services are not available, then the MSMQ windows component is not installed on the machine. The video given below shows how to install the MSMQ in Windows 7. The steps are same for Windows XP as well even though the user interface may look slightly different.


Video Steps

  1. The video invokes the ‘Control Panel’.
  2. Next, it opens the ‘Program Features’.
  3. It picks the Turn Windows Feature On/Off from the side pane.
  4. From the window displayed, MSMQ component is selected and installed.

If you are using the Windows XP, Add/Remove Windows Components should be selected as shown in Fig.3.

Add or Remove Windows Component

Add or Remove Windows Component

After installing the MSMQ component, it is a good practice to restart the machine for a smooth launch of the MSMQ services. However, this step is not a mandatory one.

3. Accessing MSMQ Through Computer Management Option

Once MSMQ windows component is installed on a machine, one can access it through the computer management utility. There are many ways available to invoke the computer management. Fig.4 shows the computer management and MSMQ location in the left side tree. In the run prompt, one can type the texts ‘mmc.exe compmgmt.mmc’. This command will invoke the Computer Management.

Accessing MSMQ from Computer Management

Accessing MSMQ from Computer Management

The below Video is loading the computer management using other two methods. In the first method, we are loading it through the Administrative tools and in the second method we are using the window explorer to load it. In Windows Explorer, we invoked the context menu from the My Computer icon then picked the menu option, Manage.


Video Steps

  1. From the start menu, the video access the administrator tool.
  2. Then, it picks the the computer management option from the displayed menu.
  3. The video closes the Computer management window.
  4. Then, it goes to Windows Explorer.
  5. After right-clicking the My Computer icon, the video picks the Manage option.
  6. From here the video navigates to MSMQ.

4. About the MSMQ Send Message Example

The screenshot of the sample is below:

About the MSMQ Send Message Example

About the MSMQ Send Message Example

When we click the send button, this example posts the details entered in the text boxes as three separate messages to the MSMQ. The send button also forms a class instance with all three details and sends it as Single message as well. There is nothing more to talk about this sample at this moment. Now we will develop this example.

This sample deals with only sending the message to MSMQ. In the next article, we will read these messages from the Microsoft Message Queue.

5. Assembly Reference to MSMQ

When we started the C# Windows application project, the IDE did not give the reference to the MSMQ DLL. Thus, we cannot access the message queuing functionalities. To bring the message queuing facilities to the application we should add assembly reference to ‘System.Messaging’. The below video shows this step:


Video Steps

  1. From the project explorer we right click reference node.
  2. Then we invoke the ‘Add reference…’ From the context menu.
  3. From the displayed dialog, we pick the Assembly binary for ‘System.Messaging’.
  4. Then in the source window, we add the using statement to make API calls.

After adding the reference to MSMQ dependencies, we include the ‘System.Messaging’ namespace to our code. Below is the code snippet:

6. Create Message Queue For Sending Messages

You can create the Message Queue from the computer management screen itself. But I will leave that to you to explore it yourself (not a big deal). Now we will create the message queue through code. First, we declare a variable that holds the Queue Name:

The ‘.’ notation says that the queue is on the local machine. You can try the queue on a remote machine by replacing the dot with system name or IP Address. Moreover, we specified that the queue is a private queue and queue name is Q1. Once we see the Q1 in the Computer Management screen, we will come to know about the above statement easily.

Next, we checked that the queue specified above exists or not. When it doesn’t exist, we create that queue. We do the checking of queue existence and creating it from the form load of the sample. This code snippet is below:

The below video shows how to ensure the above code snippet created a private queue called Q1 successfully.


Video Steps

  1. Opens Computer management MMC snap-in.
  2. Inside the Management Window Navigates to Private Queue section.
  3. Verifies form load created a queue named ‘Q1’

7. The Person Class

We will send the person class instance to the Microsoft Message Queue. To add a

Person

class, we need to right-click on the project name and select Add Class. The below code sample shows the complete

Person

class:

As we will send the object of this

Person

class as a message to the MSMQ, we stated that it should be serializable by stamping the class with an attribute,

[Serializable()]

. Also, note that we kept a public method and a default constructor. Remember, a constructor that won’t take any parameter is called a Default Constructor. We set the member which we want to include in the message body with the public scope.

8. Send Messages to Queue Q1

When we click the send button of our sample application, the code gets the reference to the queue Q1 and through that we will send the message. Note that we already ensured that the Queue Q1 exists in the form load handler. In the click event handler of the send button, we are just going to get a reference to the existing queue.

Here, we first get a reference to the queue created during the form load. Then, we create an object of type

Person

and set the details for it by calling

SetDetails

public member function. We will send this instance

p

to the message queue using the ‘default xml formator’. We will explore other formats in some other articles.

Sending a message to the message queue is done through a call to

Send

member function of the

MessageQueue

. First, we send three string messages, which are nothing but the person’s details collected from the form. All these three messages present as separate entries in the Queue Q1. Next, we send all the three-information packed inside the instance called

p

as a single message. Below is the code:

The below video shows the complete application in action:


Video Steps

  1. First we load the sample EXE.
  2. Next we enter data into the form and then click the send button.
  3. Next we navigate to Q1 and confirm that the messages are present in the Queue.

Note, reading the queue is not shown in the article. We will see that in the next article on ‘Dotnet.remoting’ section.

Source Code Download Link (Google Drive) :  Send Message MSMQ – C# Example

Categories: Remoting

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.