Recently I came across a forum query. The query was like: There is one source from which a message was sent to MSMQ and a web interface aka an asp.net page would display the newly added message automatically without any user intervention. In order to replicate the situation, I created two .NET projects; one is a simple console application and another is a web application. The console application would compose the message and send it to the MSMQ continuously using an infinite loop. The Web application connects to MSMQ and displays the message in a gridview using ajax updatepanel which is asynchronously triggered by an ajax timer control.
Time to Start with console application. I assume that you have MSMQ feature installed on your server and you are using VS 2010.
In console application, I created a very simple method to create a queue; composing and sending the message to the queue using the following code snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Threading;
namespace MSMQSample
{
class Program
{
static void Main(string[] args)
{
//Infinite loop executed in the 5 seconds interval
while (true)
{
SendMessage();
Thread.Sleep(5000);
}
}
// Method to create queue, compose message and sending message to queue
private static void SendMessage()
{
try
{
const string MSG_QUEUE = @".\private$\TestQueue";
MessageQueue _msgQueue = null;
if (MessageQueue.Exists(MSG_QUEUE))
{
_msgQueue = new MessageQueue(MSG_QUEUE);
}
else
{
MessageQueue.Create(MSG_QUEUE);
}
string _msgText = String.Format("Message sent at {0}", DateTime.Now.ToString());
Message _msg = new Message();
_msg.Body = _msgText;
_msg.Label = new Guid().ToString();
_msgQueue.Send(_msg);
}
catch (Exception exc)
{
Console.WriteLine(exc);
Read more: C# Corner
Time to Start with console application. I assume that you have MSMQ feature installed on your server and you are using VS 2010.
In console application, I created a very simple method to create a queue; composing and sending the message to the queue using the following code snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Threading;
namespace MSMQSample
{
class Program
{
static void Main(string[] args)
{
//Infinite loop executed in the 5 seconds interval
while (true)
{
SendMessage();
Thread.Sleep(5000);
}
}
// Method to create queue, compose message and sending message to queue
private static void SendMessage()
{
try
{
const string MSG_QUEUE = @".\private$\TestQueue";
MessageQueue _msgQueue = null;
if (MessageQueue.Exists(MSG_QUEUE))
{
_msgQueue = new MessageQueue(MSG_QUEUE);
}
else
{
MessageQueue.Create(MSG_QUEUE);
}
string _msgText = String.Format("Message sent at {0}", DateTime.Now.ToString());
Message _msg = new Message();
_msg.Body = _msgText;
_msg.Label = new Guid().ToString();
_msgQueue.Send(_msg);
}
catch (Exception exc)
{
Console.WriteLine(exc);
Read more: C# Corner