One way to integrate disparate systems asynchronously is to use a queue service. In ASP.NET an easy way to consume a queue is using MSMQ.

To install the Message Queue service on a Windows machine use the following command at the prompt (more info):

ocsetup MSMQ-Container;MSMQ-Server

To verify that the installation has occurred correctly, open Computer Management (compmgmt.msc) and see if appeared the Message Queuing resource like the image below:

fila02-1

With configured MSMQ. You can create an application and then consume the queue. In a Visual Studio Console project add System.Messaging DLL and also add the namespace:

using System.Messaging;

Below are some useful functions to interact with the queue service. In function Enqueue(…) simply pass the queue name, if it does not exist it will be created. And then a message is queued with the content string.

public string Enqueue(string topicName, string message)
{
    MessageQueue topic;

    if (MessageQueue.Exists(topicName))
        topic = new MessageQueue(topicName);
    else
        topic = MessageQueue.Create(topicName);

    topic.Formatter = new XmlMessageFormatter(
        new Type[] { typeof(string) });

    Message msg = new Message(message);

    topic.Send(msg);

    return msg.Id;
}

In function Dequeue(…) pass the queue name and a message will be removed and returned. The code below has two attention points:

  1. Note the command topic.MessageReadPropertyFilter.SetAll() which defines that the readings should bring all the message properties.
  2. The message is read by topic.Receive(…) that takes an argument of type TimeSpan with the value of 1 second. For when the queue is empty and we call the function topic.Receive(…) without setting the waiting time the function will wait until the arrival of an message.
public System.Messaging.Message Dequeue(string topicName)
{
    MessageQueue topic;

    if (MessageQueue.Exists(topicName))
        topic = new MessageQueue(topicName);
    else
        topic = MessageQueue.Create(topicName);

    topic.Formatter = new XmlMessageFormatter(
        new Type[] { typeof(string) });

    topic.MessageReadPropertyFilter.SetAll();

    try
    {
        Message message = topic.Receive(
            TimeSpan.FromSeconds(1), 
            MessageQueueTransactionType.None);

        return message;

    }
    catch (Exception ex)
    {
        return null;
    }
}

You can also completely delete the data in a queue using the method Purge().

public void Purge(string topicName)
{
    MessageQueue topic;
    if (MessageQueue.Exists(topicName))
        topic = new MessageQueue(topicName);
    else
        topic = MessageQueue.Create(topicName);

    topic.Purge();
}

Please note: The queue name has a specific format. A name example would be .\Private$\teste01 like the image below:

fila01

Some ways to use the above functions:

//
// Enqueue the string "data" then returns the message ID
//
string id = Enqueue(".\private$\teste01", "data");

//
// Removes the oldest item in the queue then returns
//
string newData = Dequeue(".\private$\teste01");

//
// Purge all data
//
Purge(".\private$\teste01");