using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Messaging;
namespace Demo
{
public partial class WebForm9 : System.Web.UI.Page
{
MyNewQueue myNewQueue = new MyNewQueue();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
myNewQueue.SendMessage();
}
protected void Button1_Click(object sender, EventArgs e)
{
myNewQueue.ReceiveMessage();
}
}
public class Order
{
public int orderId;
public DateTime orderTime;
};
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
private MessageQueue myQueue;
public MyNewQueue()
{
if (!MessageQueue.Exists(".\\private$\\myQueue"))
MessageQueue.Create(".\\private$\\myQueue");
myQueue = new MessageQueue();
myQueue.Path = ".\\private$\\myQueue";
}
public void SendMessage()
{
Order sentOrder = new Order();
sentOrder.orderId = 3;
sentOrder.orderTime = DateTime.Now;
myQueue.Send(sentOrder);
return;
}
public void ReceiveMessage()
{
myQueue.Formatter = new XmlMessageFormatter(new Type[]{typeof(Order)});
try
{
Message myMessage = myQueue.Receive(new TimeSpan(0, 0, 3));
Order myOrder = (Order)myMessage.Body;
HttpContext.Current.Response.Write("Order ID: " + myOrder.orderId.ToString());
HttpContext.Current.Response.Write("Sent: " + myOrder.orderTime.ToString());
}
catch (MessageQueueException)
{
}
// Handle invalid serialization format.
catch (InvalidOperationException e)
{
}
return;
}
}
}