MSMQ is used when data needs to be communicated between different applications.
---
title: Introduction to MSMQ
subtitle: MSMQ is used when data needs to be communicated between different applications
author: dotnetlearners
date: March 20, 2019
source: http://dotnetlearners.com/msmq
notoc: false
---
## Intro
Microsoft Message Queuing ([MSMQ]) technology will be used when data need to be
communicate between different application(or same application) at different
times.
**There are different types of Message Queues**:
- Public Queue
- Private Queue
- System Queue
## Configure MSMQ
Go to the **Control Panel** -> **Programs and Features** -> select **Windows features**, turn **on**/**off**.
![Windows Features > **Microsoft Message Queue (MSMQ) Server**](assets/introduction_to_msmq/install.png)
To check whether **MSMQ** is installed or not... right-click on **My Computer**
and select Manage. Expand Services and **Applications** -> **Message Queuing**.
![Computer Management > **Message Queuing**](assets/introduction_to_msmq/msmq.png)
## Create Message Queue
- Right click on **My Computer** and select manage.
- Expand **Services** and **Applications** -> **Message Queuing** -> **Private Queues**.
- Right click on **Private Queues** -> **New** -> **Private Queue**
![Computer Management > Message Queuing > New > **Private Queue**](assets/introduction_to_msmq/Createmessagequeue1.png)
Enter the **name the Queue**, select **Transactional**, then click **OK**.
![New Private Queue > **Queue Name**](assets/introduction_to_msmq/Createmessagequeue2.png)
## Insert Message in Message Queue
The following sample code will give you an idea that, how to insert messages in
Message Queue.
1. Add a reference to the `System.Messaging` assembly
2. Add the `using System.Messaging;` statement.
```cs
private void InsertMessageinQueue()
{
try
{
string messageQueueName = @"FormatName:DIRECT=TCP:192.168.1.3\\Private$\\pvtmsmq";
System.Messaging.MessageQueue messageQueue = new System.Messaging.Message(messageQueueName);
MessageQueue.Send("SampleMessage", System.Messaging.MessageQueueTransactionType.Single);
MessageQueue.Close();
}
catch
{
throw;
}
}
```
## Retrieve Message from Message Queue
The following sample code will give you an idea that, how to read messages from
Message Queue.
1. Add a reference to the `System.Messaging` assembly
2. Add the `using System.Messaging;` statement.
```cs
private string RetriveMessageFromQueue()
{
try
{
string messageQueueName = @"FormatName:DIRECT=TCP:192.168.1.3\\Private$\\pvtmsmq";
string messageQueueValue = string.Empty;
System.Messaging.MessageQueue messageQueue = new System.Messaging.MessageQueue(messageQueueName);
System.Type[] messageTypes = new System.Type[1];
messageTypes[0] = messageQueueValue.GetType();
messageQueue.Formatter = new System.Messaging.XmlMessageFormatter(messageTypes);
messageQueueValue = messageQueue.Receive(new TimeSpan(0, 0, 5), System.Messaging.MessageQueueTransactionType.Single).Body.ToString();
return messageQueueValue;
}
catch
{
throw;
}
}
```
## Message Queue Notes
If you want to access the message queue with in the system then you can declare the MSMQ as:
```cs
System.Messaging.MessageQueue messageQueue = new System.Messaging.MessageQueue(
@".\Private$\pvtmsmq");
```
If you want to access the message queue with in LAN system then you have to declare the MSMQ as:
```cs
System.Messaging.MessageQueue messageQueue = new System.Messaging.MessageQueue(
@"FormatName:DIRECT=TCP:192.168.1.3\\Private$\\pvtmsmq");
```
[MSMQ]: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx