Work with Windows Message Queue in C# (MSMQ C#)

Introduction

Microsoft Message Queuing (MSMQ) is a technique that enables computers or applications which run over heterogeneous networks to send messages and receive messages from the Windows message queue.  Here this blog post explores the MSMQ queue and has some code examples of message queuing services.

msmq c#

Microsoft Message Queuing (MSMQ)

Getting Started

MSMQ is developed by Microsoft to implement Windows message queues. The MSMQ queue is a temporary storage location from which messages can be sent and received reliably. The message queuing service provides guaranteed message delivery between applications, this delivery can be prioritized according to your needs.

MSMQ service has been available to developers on Microsoft platforms since 1997 and has commonly been used in enterprise software built with Visual Studio. The Windows Communication Foundation (WCF) is one of the examples of MSMQ.

Two Services play an important role in the MS message queue, these are Message Queuing Service (MSMQ Service) and Message Queuing Triggers (MSMQTriggers).

Message Queuing Service (MSMQ Service)

Provides a messaging infrastructure and development tool for creating distributed messaging applications for Windows-based networks and programs. If this service is stopped, distributed messages will be unavailable. If this service is disabled, any services that explicitly depend on it will fail to start.

Message Queuing Triggers Service (MSMQTriggers)

Provides rule-based monitoring of messages arriving in a Message Queuing queue and, when the conditions of a rule are satisfied, invokes a COM component or a stand-alone executable program to process the message.

Demonstrations of MSMQ in C#

The following demonstration creates message queue, sends, receives, and fetches all the available messages from queue. Make sure that you have installed the MSMQ Server otherwise you will get the exception message queueing has not been installed on this computer.

msmq service

Message Queuing has not been installed on this computer

Create a MSMQ

The following code block creates MSMQ using C# code by creating instances of message queuing class. Mainly based on accessibility there are two types of queue Public Queue and Private Queue. The below examples create both types of MSMQ.

Public MSMQ
 System.Messaging.MessageQueue msmq;  
 if (MessageQueue.Exists(@".\\Sale"))  
 //creates an instance MessageQueue, which points   
 //to the already existing Sale  
 msmq = new System.Messaging.MessageQueue(@".\\Sale");  
 else  
 //creates a new private queue called Sale   
 msmq = MessageQueue.Create(@".\\Sale");  
Private MSMQ
 System.Messaging.MessageQueue msmq;  
 if (MessageQueue.Exists(@".\Private$\Sale"))  
 //creates an instance MessageQueue, which points   
 //to the already existing Sale  
 msmq = new System.Messaging.MessageQueue(@".\Private$\Sale");  
 else  
 //creates a new private queue called Sale   
 msmq = MessageQueue.Create(@".\Private$\Sale");  

You can verify whether the queue is created or not with the Computer Management Console. Expand, Services and Applications in Computer Management Console. Expand, Message Queuing and click Private Queue. You’ll find Sale here. Now, let us see how to send the messages.

msmq

Windows Message Queue


Set Permission

The following code examples demonstrate how to set permission for a queue. This code is applicable to both private and public queues. code.

 // Create an AccessControlList.  
 AccessControlList list = new AccessControlList();  
 // Create a new trustee to represent the "Everyone" user group.  
 Trustee tr = new Trustee("Everyone");  
 // Create an AccessControlEntry, granting the trustee read access to  
 // the queue.  
 AccessControlEntry entry = new AccessControlEntry(  
      tr, GenericAccessRights.All,  
      StandardAccessRights.All,  
      AccessControlEntryType.Allow);  
 // Add the AccessControlEntry to the AccessControlList.  
 list.Add(entry);  
 // Apply the AccessControlList to the queue.  
 msmq.SetPermissions(list);  

Send a message

The following code example shows how to sends an object to MSMQ as a message, you can send a string value also. The code is applicable to both types of MSMQ.

 Sale mySale;  
 mySale.To = "Kailash";  
 mySale.By = "Nidhi";  
 mySale.Amount = 1000;  
 mySale.SaleDate = "2023-06-14";  
 System.Messaging.Message msg = new System.Messaging.Message();  
 msg.Body = mySale;  
 msmq.Send(msg);  

Receive a Message from Windows Queue

The following code example receives a message from a queue and convert to the sale object. This call is synchronous, and blocks the current thread of execution until a message is available.

 MessageQueue msmq = GetMSMQInstance();  
 Sale mySale;  
 System.Type[] arrTypes = new System.Type[1];  
 arrTypes[0] = mySale.GetType();  
 msmq.Formatter = new XmlMessageFormatter(arrTypes);  
 mySale = ((Sale)msmq.Receive().Body);  

Get Alll Messages

The following code fetches all the messages available in queue. GetAllMessages returns a static snapshot of the messages in the queue, not dynamic links to those messages. Therefore, you cannot use the array to modify the messages in the queue.

 // Populate an array with copies of all the messages in the queue.  
 Message[] msgs = msmq.GetAllMessages();  
 // Loop through the messages.  
 foreach(Message msg in msgs)  
 {  
   // Display the label of each message.  

Install Message Queuing On Windows 10

Follow the below instructions to install MSMQ on windows 10 .

  1. Open Windows Control Panel. Select program, and then select programs and features.
  2. Choose “Turn Windows features on or off.” Then, select “Microsoft Message Queue (MSMQ) Server.”
  3. Click on the taskbar search option and search for “Computer Management.” Select all the check boxes as shown in the below image.
  4. Work with Windows Message Queue in C#

    Install Message Queuing Windows 7 or Windows Vista

    1. Open Control Panel.
    2. Click Programs and then, under Programs and Features, click Turn Windows Features on and off.
    3. Expand Microsoft Message Queue (MSMQ) Server, expand Microsoft Message Queue (MSMQ) Server Core, and then select the check boxes for the following Message Queuing features to install:
      1. MSMQ Active Directory Domain Services Integration (for computers joined to a Domain).
      2. MSMQ HTTP Support.
    4. Click OK.
    5. If you are prompted to restart the computer, click OK to complete the installation

    install Message Queuing on Windows Server 2008 or Windows Server 2008 R2

    1. In Server Manager, click Features.
    2. In the right-hand pane under Features Summary, click Add Features.
    3. In the resulting window, expand Message Queuing.
    4. Expand Message Queuing Services.
    5. Click Directory Services Integration (for computers joined to a Domain), then click HTTP Support.
    6. Click Next, then click Install.

    Install Message Queuing on Windows Server 2012 or 2016

    1. Open Server Manager.
    2. Click Add Roles and Features.
    3. Click Installation Type.
    4. Click Role-based or feature-based installation.
    5. Click Next.
    6. Click Features.
    7. Expand Message Queuing.
    8. Click Message Queuing Server.
    9. Click Directory Service Integration.
    10. Click HTTP Support.
    11. Click Next.
    12. Click Install.

    Summary

    Microsoft Message Queuing is a message queue implementation developed by Microsoft and deployed in its Windows Server operating systems since Windows NT 4 and Windows 95. Here this blog post, demonstrated Create,send, receive messages and install MSMQ. I hope you have endjoyed this.

    Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال