ASP.NET Podcast Show #33 - MSMQ
Wally has done another good job, this time concentrating on the Microsoft Message Queue in .Net apps.
Original URL: http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2006/01/10/71.aspx
ASP.NET Podcast – Show #33 - MSMQ
Show Notes:
Welcome to the First Show of 2006.
More Wally in your life.
Wally got his MVP Award for 2006 and goes off on a tangent about what an MVP is.
Wally (and all newly awarded MVP's) get a really cool laptop bag as a present.
The “Beginning AJAX with ASP.NET” book is coming along. Paul, Scott, and I are hard at work.
Who is Bruce Dickenson and why does he send me strange emails? Do you know why?
What do words mean in IT?
- Strategic.
- Tactical.
- Opportunity.
- “Stepping up to the plate.”
- “Stepping into one”/”Taking one for the team.”
MSMQ
Class:
[Serializable()]
public class cSearchResults
{
public string Url = String.Empty;
public string ServerName = String.Empty;
public long Id = 0;
public string UrlText = String.Empty;
public DateTime DateSearched;
}
Sending a Message:
Simple:
public static void StoreSearchUrl( string pstrCn, string pstrUrl )
{
MessageQueue mq = new MessageQueue(gstrQueueForSearchUrl);
mq.Send(pstrUrl);
mq.Dispose();
mq = null;
}
Class:
MessageQueue mq = new MessageQueue(gstrQueueForSearchResults);
cSearchResults objRes = new cSearchResults();
try
{
objRes.UrlText = pText;
objRes.Url = pstrUrl;
objRes.Id = plngUrl;
objRes.DateSearched = DateTime.Now;
mq.Send(objRes);
}
finally
{
mq.Dispose();
mq = null;
}
Receiving a Message:
Simple:
gMQSearchUrl = new MessageQueue();
gMQSearchUrl.Path = gstrQueueForSearchUrl;
gMQSearchUrl.Formatter = new XmlMessageFormatter(new string[] { "System.String, mscorlib" });
gMQSearchUrl.ReceiveCompleted += new ReceiveCompletedEventHandler
this.MQ_ReceiveCompleted_ForSearchUrl);
gMQSearchUrl.BeginReceive();
...............
private void MQ_ReceiveCompleted_ForSearchUrl(object sender, ReceiveCompletedEventArgs e)
{
// Add code here to respond to message.
//new XmlMessageFormatter(new String(){"System.String, mscorlib"});
System.Messaging.Message msg = gMQSearchUrl.EndReceive(e.AsyncResult);
msg.Formatter = new XmlMessageFormatter(new String[]{"System.String, mscorlib"});
string strBody = (string)msg.Body;
try
{
StoreUrlInSearchUrl(strBody);
}
//Exception handling code
finally
{
if ( this.gblRunStatus == true )
{
gMQSearchUrl.BeginReceive();
}
msg.Dispose();
msg = null;
}
Class:
gMQSearchResults = new MessageQueue();
gMQSearchResults.Path = gstrQueueForSearchResults;
gMQSearchResults.Formatter = new XmlMessageFormatter(new Type[] { typeof
WebSearch.cSearchResults) });
gMQSearchResults.ReceiveCompleted += new ReceiveCompletedEventHandler(this.MQ_ReceiveCompleted_ForSearchResults);
gMQSearchResults.BeginReceive();
........................
private void MQ_ReceiveCompleted_ForSearchResults(object sender, ReceiveCompletedEventArgs e)
{
// Add code here to respond to message.
System.Messaging.Message msg = gMQSearchResults.EndReceive(e.AsyncResult);
WebSearch.cSearchResults cSearchRes;
try
{
msg.Formatter = new XmlMessageFormatter(new Type[] {typeof(WebSearch.cSearchResults)}); //= new XmlMessageFormatter(new Type() {GetType(WebSearchSupport.cSearchResults)}); //new XmlMessageFormatter(new String(){"System.String, mscorlib"});
cSearchRes = (WebSearch.cSearchResults)msg.Body;
StoreUrlInSearchResults(cSearchRes);
}
//Exception handling code
finally
{
msg.Dispose();
msg = null;
if ( this.gblRunStatus == true )
{
gMQSearchResults.BeginReceive();
}
}
}