http://www.codeproject.com/Articles/143454/ASP-NET-page-automatically-notifies-when-MSMQ-mess
1.In console application, I created a very simple method to create a queue; composing and sending the message to the queue using 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 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); } } } }
2 . 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
<%@ 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
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; } } } }