Recently, I have come across one forum query. The query was like: There is one source from which 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 project; one is simple console application and another one is an web application. Console application would compose the message and send it to the MSMQ continuously using an infinite loop. Web application connects to MSMQ and display the message in a gridview using ajaxupdatepanel which asynchronous trigger fired by an ajax timer control.
Time to Start with a 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 followingcode snippet.
Collapse
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 sendingmessage to queue
private static void SendMessage()
{
try
{
const stringMSG_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("Messagesent at {0}", DateTime.Now.ToString());
Message _msg = new Message();
_msg.Body = _msgText;
_msg.Label = new Guid().ToString();
_msgQueue.Send(_msg);
}
catch (Exceptionexc)
{
Console.WriteLine(exc);
}
}
}
}
Running this console application would create the message and send the message to MSMQ.
Now you need to create one web application with one aspx page. The aspx page contains one ajax timer,ajax updatepanel and gridview control. Iuse timer control to invoke the method on each tick event(interval of 5seconds) of it and update the gridview control using partial update(because I put the gridview withing updatepnel).
I also one custom class MSMQMessage to store the message label and body. You can add/remove propertiesfrom it according to your requirement. Basically, I am binding list of MSMQMessage with the gridview control.
The aspx code looks like
Collapse
<%@ Page Language="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="MSMQSample.Web.Default"%> <!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>title> head> <body> <form id="form1″ runat="server"> <asp:ScriptManager ID="ScriptManager1″runat="server"> asp:ScriptManager> <div> <asp:Timer ID="UpdateTimer"runat="server"Interval="5000″OnTick="UpdateTimer_Tick">asp:Timer> <asp:UpdatePanel ID="UpdatePanel1″runat="server"> <Triggers> <asp:AsyncPostBackTriggerControlID="UpdateTimer"EventName="Tick"/> Triggers> <ContentTemplate> <asp:GridView ID="grdMessage"runat="server"> asp:GridView> ContentTemplate> asp:UpdatePanel> div> form> body> html> The MSMQMessage classcode is using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MSMQSample.Web { public class MSMQMessage { public string Label {get;set;} public string Body{get;set;} } }
In codebehind, I write a method to read the message from queue, bind the gridview with list of message and call this method on tick event of timer. This is achieved using following code
Collapse
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Messaging; using System.Threading; namespace MSMQSample.Web { public partial class Default :System.Web.UI.Page { const stringMSG_QUEUE = @".\private$\TestQueue"; protected voidPage_Load(object sender, EventArgs e) { if(!IsPostBack) { UpdateGUI(); } } protected voidUpdateTimer_Tick(object sender, EventArgs e) { UpdateGUI(); } private voidUpdateGUI() { try { MessageQueue _msgQueue = null; IList<MSMQMessage>_msgList = new List<MSMQMessage>(); if (MessageQueue.Exists(MSG_QUEUE)) { _msgQueue = new MessageQueue(MSG_QUEUE); } else { throw newException("MessageQueue does not exist"); } foreach (Message_message in _msgQueue.GetAllMessages()) { MSMQMessage _msmqmessage = new MSMQMessage(); _msmqmessage.Label = _message.Label; _message.Formatter = new XmlMessageFormatter(newString[] { "System.String,mscorlib"}); _msmqmessage.Body = _message.Body.ToString(); _msgList.Add(_msmqmessage); } grdMessage.DataSource = _msgList; grdMessage.DataBind(); } catch { throw; } } } }
It is obvious that the update GUI method would be called by the Timer’s tick event in the interval of 5 seconds and it will bind the list of message with the gridview.
How to test the application. First run the aspx page. Initially it will not display anything because you don’t have any message in the queue.
Right click on console application from solution explorer and click on Debug à Start New Instance.
That’s it. Now you will show the aspx page automatically refreshes the gridview and shows you the latest messages
No comments:
Post a Comment