Commerce

How to Publish and Subscribe Messages to a Queue Using MSMQ

Now that you’ve learned how to install MSMQ technology in your technology stack, you can now set up publishing and subscribing to messages as well. Follow the steps below to set up the code for publishing, which allows the sender application to send the message to the queue.

Step 1: Create the Publisher Application. For this example, I’ve used the Rest API application to publish a message to the queue.

Open the Visual Studio, choose “create a new project,” select “Asp.Net Web Application (.Net Framework),” enter the project name, and choose “Web API.”

Step 2: Add the System.Messaging Reference. Right-click on the “references” option and select “add a reference.”

Step 3: Create the API Controller. For this example, I used the “OrderRefreshController.”

Step 4: Create the entity for the message. We can pass a message as an object to senders with the structure below with the Serializable attribute .net code to use on the sender side.

Step 5: Write the line of code below to send the message to the queue.

Step 6: Execute the above function from the Rest API POST Method.

Step 7: Run and test the application.

Subscribing Messages Using MSMQ

When you subscribe to a message, you can receive and read the message from the queue after the message is published to the queue.

Follow the code below to implement the subscription to the queue.

Step 1: To subscribe to the messages, I have created the Windows service computer program that operates in the background of the Windows machine or Windows server so that I can install and start on the Windows server. This will keep it running and read the message from the queue as the message published to the queue.

Step 2: Once the Windows service application is created, add the service reference (System.Messaging).

Below is what the Windows service project structure will look like:

Note: I have renamed “Service1.cs” to “SubscribeOrderRefreshQueue.cs” to follow the naming convention. You can use any standard name as per your project naming convention.

Step 3: Add the class file “QueueHelper,” which consists of the methods below:

  • ConnectToQueue: Used to connect to the MSMQ
  • MyReceiveCompleted: Responsible for reading the message when it’s published.

Refer to the below screenshot for the code snippet:

Step 3: Use the method from the windows service. Right-click on service class “SubscribeOrderRefreshQueue.cs” and select “view code.” The “On-start method” will write the code and execute the method “MyReceiveCompleted.

Note: I have added the method “RunInteractive” so that I can run the Windows service in the interactive (debug) mode.

Below are the changes to run the Windows service is debug mode:

  1. Right-click on the “Windows service project” and navigate to “properties.” In the application tab, change the output type to “console application.”

  1. Add the line code below in the “cs file.”

Now you are all set to run the Window service and test the publish and subscription options. See the below video to understand the flow from publishing messages to subscribing/reading the message. In the video, the Postman app calls the application, which pushes the message to the queue, and the Windows Service application will receive and process it. This way, MSMQ acts as a middleman and establishes the communication between the two applications (source and destination) without having a direct connection between each other.

For more information on setting up these services in your technology stack, contact our commerce experts today. 

About the Author

Nitin Garg is a Lead Technical Consultant with a demonstrated history of working in the information technology and services industry. He has over 10 years of experience in the eCommerce domain using the platform like (Insite Commerce & Znode B2B commerce) and .Net based application.

More from this Author

Leave a Reply

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