asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Leveraging MSMQ in ASP.NET Applications
By Greg Huber
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Have you ever developed a Web application that requires extensive processing? Ever had long running Web pages that often time out in the browser? Most Web developers run into these kinds of issues at some point. As Web application requirements evolve, processing and business logic tend to get more complex. Consequently, it is no surprise that Web applications can lead to frustrating Web experiences.

    On the other hand, there may also be processes that simply take time no matter how simple or complex the application. At times, there is processing that typically does not lend itself to occur in a Web application.

    The above scenarios require an effective way to initiate long running processes so that inherit problems due to the synchronous nature of the Web and long running processes are minimized or avoided all together.

    Requirements

    .NET Framework 1.1 (Visual Studio .NET helpful but not required)
    Basic familiarity with ASP.NET, .NET Framework
    Windows 2000 Professional/Server or Windows XP or 2003 Server

    Synchronous vs. Asynchronous Programming Concepts

    Before diving into the code, it's important to understand a few key concepts regarding programming models. Let's start out with the Synchronous model. In its simplest form, this programming model means that the caller waits until all processing has completed and control is returned.

    Alternatively, asynchronous programming means that the caller makes a call and proceeds immediately. A "callback" located in the callers process space may be invoked by the recipient if desired (and available).

    The asynchronous concept not only applies to programming models, but also to architectures. Similar to the programming model, a Web application can be architected in such a way that an asynchronous model is achieved. Hence, many of the limitations of the synchronous nature of the Web can be dealt with. In this article, MSMQ will be leveraged to show how to implement such a model.

    Overview of MSMQ

    MSMQ is the abbreviation for "Microsoft Message Queue". A queue is simply a message store that is accessible via an API. In .NET, the System.Messaging library is used to interface with an MSMQ.

    The primary purpose of MSMQ is to allow for implementing reliable, scalable, high performance distributed applications. Microsoft first released MSMQ with Windows NT 4.0, implemented via COM components. This initial release was fairly complex to deal with in a programmatic fashion. Nevertheless, it introduced many important concepts and benefits, which have been improved upon and carried forward to the current version of MSMQ. Considerable performance gains have also come about since MSMQ 1.0.

    For more information, previous version and current features, and support information on MSMQ, etc. please visit the Microsoft MSMQ Center. Also visit the article titled "Accessing Message Queues" for information on reading messages.

    Getting Started

    The remainder of this article will pose a common scenario, a solution with MSMQ, and walk through the necessary steps to implement the solution.

    A Common Scenario

    Over time, business requirements evolve, and applications need to be robust enough that they can keep up. Consider the case where a complex calculation evolves to the point where a Web application can no longer process in "Web-time". In fact, as requirements evolve, data for such a calculation may even come from another system all together, which certainly complicates things.

    In other cases, an application you are planning on developing may be a great fit for the Web, with the exception of one or two back-end pieces -- for example, generating a report, moving large amounts of data from a data source, etc. These are often activities that Web applications need at some point in time, but due to the lengthy processing time, the Web doesn't lend itself well.

    Unfortunately, there may be little you can do to increase performance / response times in these kinds of scenarios. Often, developers resort to creating a thick-client Windows application that can reside on a desktop-which allows for more control over timeouts. However, many complexities are also introduced, i.e. processing moves from the server to the client and numerous issues may arise including scalability, security, deployment, etc.

    One great solution is implementing an asynchronous programming model using MSMQ.

    The Proposed Solution: "Disconnected Processing" with MSMQ

    At a high level, there are three major logical components. These components may exist on the same machine or could be on completely separate machines.

    The ASP.NET client sends a message to the message queue and moves on its merry way. The MSMQ component is a simple message broker; it allows the reading and writing of messages. When a message is read, it is removed from the queue. The .NET service monitors the MSMQ and processes as soon as a message is received. The messages will pile up in the queue until the .NET service is ready to process the next message.

    Pre Requisite: Setting up MSMQ

    Ensure that you have your Windows 2000/XP CD handy. Simply open the control panel, "add/remove" Windows components, and ensure the Message Queuing Services checkbox is checked. Message queuing must be installed on both the sending (client) and receiving machine (server).

    After this step is completed, you will be able to create a private message queue. A private queue is for "workgroup installations", meaning the computer is not integrated in the Active Directory. There are some limitations that exist in private queues-they are not published whereas public queues are. For a complete list of framework methods that will not operate in private queues, please see the following MSDN url.

    Platform SDK: Message Queuing Offline Reference

    Open up the Microsoft Management Console, add/remove the "Computer Management" snap-in, browse to "Services and Applications", and expand and select the "Message Queuing" node. Right click on "Private Queues" and create a new queue.

    Code Walkthrough: Writing a message from the Web

    Now that you have set up a private message queue to send and receive messages, you can easily write to it with a Web application. There are a few simple steps to get started. The code for these steps have been included as well-simply unzip to a virtual directory and modify the web.config with your settings.

    Important Steps for setting using Messaging in an ASP.NET page

    Visual Studio.NET MethodFramework Method
    Add a reference in your Solution to System.Messaging:
    Explicitly add the assembly to your web.config under the :

    ** Note, you can verify the assembly information by using "gacutil" tool.

    Add the line to the top of the class:

    using System.Messaging;

    Add the line to the top of the page:

    <%@ Import Namespace="System.Messaging" %>

    After completing the above steps, your code will be ready to interact with the MSMQ. For demonstration purposes, you can easily create an ASP.NET page with a simple user interface to send a message to a message queue:

    This interface shows the two important elements that are necessary for sending messages to an MSMQ-the message queue path and the message itself.

    Message Queue Path
    A message queue path string must be properly constructed. The syntax of the path can vary widely, depending on the type of queue you are talking to, and where it is located. In the above example, the MSMQ resides on the same machine as the Web server. However, in realistic situations, the MSMQ will reside on another machine. Examples of queue paths:

    Queue TypeLocation message is sent fromExample
    Public Queue (must belong to ActiveDirectory)AnywhereServerName\QueueName
    Private QueueSame server MSMQ resides on.\private$\QueueName
    Private QueueAnyremote locationFormatName:DIRECT=OS:machinename\private$\queuename

    Message
    When using MSMQ with .NET, a message can be any serializable object. When serializing a message to MSMQ, you can use different formatters (including Binary, XML, or even ActiveX which allows for sending COM objects-useful if you want to tap into an existing MSMQ aware application based on COM). You can specify the formatting of your message through the message object that you are sending. For more information on message options, please see the MSDN article on Accessing Message Queues.

    In the examples in this article, the default formatter (XML serialization) is used to send a message to the queue, and the message is simply a string object. As mentioned previously, this message could be anything serializable, such as a business object that could then be consumed directly by the listener process.

    Code Snippet: Sending a Message

    
    MessageQueue MyMessageQ;
    	Message MyMessage;
    
    	MyMessageQ = new MessageQueue(txtQueuePath.Value);
    	MyMessage = new Message(txtMessageToSend.Value);
               MyMessageQ.Send(MyMessage);		
    	divMessage.InnerHtml= "<b>Message sent 
    successfully!</b>";
    
    
    In this scenario, one important reason for using MSMQ is to provide the capability to separate out lengthy processing from the user. Consequently, after sending a message, the Web page has completed processing and control is returned immediately to the user.

    Code Walkthrough: Receiving & Processing a Message

    To receive a message with MSMQ in the given scenario, a separate process must be running that is watching the queue. A great way to implement this process is to create a simple .NET service that listens to the queue and processes as soon as a message is sent to it.

    Code Snippet: Receiving a message in the .NET service:

    
    	MessageQueue MyMessageQ;
    		Message MyMessage;
    
    		MyMessageQ = new MessageQueue(_QueuePath);
    		MyMessage = MyMessageQ.Receive;	
    		WriteStatus("Message Received!");
    		DoSomeLongRunningProcess();
    		WriteStatus("Processing Finished!");
    
    
    The "DoSomeLongRunningProcess" method contains code that performs a process that would otherwise cause a timeout if a Web page were running it. Throughout the processing of this method, a database table will be updated (shown later) with status information so at any given point in time, the status of the processing can be determined.

    In order to install the service included in the source code, you can simply run the following command after shelling out to the command line:

    C:\WINNT\Microsoft.NET\Framework\v1.1.4322\installutil msmqService_listener.exe

    Upon successful installation, you will see some informational text scroll by and a confirmation message:

    "The Commit phase completed successfully.

    The transacted install has completed."

    After installing the listener service, you can open the administrative control panel and browse the current Windows services running on your machine. You should see the service you just installed as pictured below.

    After unzipping the service, you will want to ensure that the paths in the MSMQService_Listener.exe.config are correct (instructions are in this file- which is located in the bin directory).

    You can now start your service by right clicking "start" and be on your merry way!

    Other Considerations

    The message queue Receive method waits indefinitely until a message is received on the queue. Depending on the type of processing you are doing, you may want to queue up some worker threads so that multiple messages can be received and processed at once. Consequently, this type of processing can be very robust and scalable.

    There are a variety of other methods on the message queue object certainly worth investigating. The following are a few that are noteworthy:

    Method NameDescription
    PeekPerforms a non-destructive read a message, meaning the message is not deleted after "peeking". (Read purges the message)
    BeginRead / EndRead, BeginPeak/ End PeakAllows for asynchronous handling of the message. Code will continue running while a message is being received; an event handler will execute when the message has been fully received.
    DeleteDeletes a message out of the queue.

    Code Walkthrough: Checking Status Information

    As hinted above, in the scenario where a Web page has triggered a long running process, a user may want status information. One simple technique for providing status information would be to write information (from the .NET service that is doing the processing, as mentioned above) to a database table. The Web application can then check this database table at the request of the user to determine current status.

    Database Table

    The table above is a simple table that keeps track of a Queue Name and current status. In a real world scenario, you would most likely have more information that you may want to keep track of (such as errors that occurred, information about the message received, etc.). When the listener service receives a message, this status table is updated throughout the processing that occurs.

    Code Snippet: Viewing the Table

    
    string dbConnString = 
    ConfigurationSettings.AppSettings["AccessDBConnectionString"]; 
    
    //Create connection object 
    OleDbConnection oCon = new OleDbConnection(dbConnString); 
    string sSQL = "Select * FROM QueueStatus ORDER By ID Desc"; 
    OleDbCommand oCmd= new OleDbCommand(sSQL, oCon); 
    oCon.Open(); 
    
    dgStatus.DataSource = oCmd.ExecuteReader(); 
    dgStatus.DataBind();
    
    
    In the source code provided, a Web-based interface reads this table and simply displays the current status. To get the latest status, the user must hit the "show status" button.

    Here is a sample view of seeing how the status has progressed:

    Conclusion

    This article demonstrated a simple technique for handling long running Web processes through the Web via Microsoft MSMQ and the System.Messaging framework. MSMQ is a very useful product that developers should consider leveraging in Web applications.

    About the Author

    Greg Huber is the President and Founder of the Northwest Ohio .NET Users group (http://www.nwnug.com). He is active in the .NET community-speaking at area user groups, serving on the INETA (http://www.ineta.org) user group relations committee, and spearheading community-oriented development initiatives, including Quizzard, a shared-source .NET quiz game. He has been developing and architecting software on the Microsoft platform since 1998, and is currently a lead developer at NFO Worldgroup.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    XCache
    XCache combines dynamic content caching technology with content delivery network (CDN) support options, file compression and a whole lot of manageability features to help e-businesses deliver superior web site performance and reliability. You'll appreciate the administrative ease, your visitors will appreciate increased page delivery speed.
    [Top]
    XCompress
    XCompress works by compressing outgoing text between the Web server and the client. Page response times may improve by a factor of three or more while overall bandwidth use can drop by two thirds or more.

    XCompress runs on Windows 2000 and Windows NT 4.0 and is tightly integrated with Microsoft Internet Information Server (IIS) with MMC and COM interfaces.

    [Top]
    XTune
    XTune 2.0 is the most powerful tuning application for IIS 4 or IIS 5 ever conceived. Indispensable to the enterprise and straightforward, this web tuning tool allows you to configure hidden operating system, network, Active Server Pages and Internet Information Server settings for better performance, without any additional hardware or software.

    This version scans your system more deeply, offering more performance-enhancing recommendations and greater insight into your web architecture. The Performance Wizard guides and teaches you throughout the complete tuning process, so you can learn while making your box run better than ever.

    Purchase here.

    [Top]
    Other Articles
    Aug 25, 2005 - Performance Monitoring in SharePoint Portal Server 2003
    Performance monitoring helps organizations identify performance bottlenecks. The problem is that with so many performance numbers available, how do you know which ones to watch? This article helps you identify which are the critical performance counters in a SharePoint Portal Server environment and explains how to monitor them. By monitoring performance regularly, organizations can recognize performance trends as they develop and prevent problems before they get out of hand.
    [Read This Article]  [Top]
    Aug 12, 2004 - Middle-Tier Hosting: Enterprise Services, IIS, DCOM, Web Services, and Remoting
    There is broad-reaching debate about remoting, Web services, Enterprise Services, and DCOM. In short, it is a debate about the best technology to use when implementing client/server communication in .NET. Rocky Lhotka shares his thoughts on the issue while offering clear explanations of basic application architecture terminology.
    [Read This Article]  [Top]
    May 18, 2004 - ASP.NET 2.0 Caching Features
    This article examines some of the new and exciting caching features in ASP.NET 2.0 and shows how to implement them in Web applications.
    [Read This Article]  [Top]
    Feb 12, 2004 - Case Study: Match.com
    When it came time to find a technology for its massive upgrade, Match.com chose .NET. Has the online dating service's partnership with Microsoft been as successful as the relationships it has established for many of its millions of members? Read on ...
    [Read This Article]  [Top]
    Jan 15, 2004 - Database Performance Philosophy
    Longtime 15Seconds discussion list member Tore Bostrup offers valuable advice on designing databases and applications for efficient querying.
    [Read This Article]  [Top]
    Dec 29, 2003 - Caching Oracle Data for ASP.NET Applications
    Narayan Veeramani shows how ASP.NET developers can improve application performance by caching data stored in an Oracle database and keeping the cached data in sync with the data in the Oracle database.
    [Read This Article]  [Top]
    Mar 14, 2002 - Web Site Compression
    As IT professionals try to reduce the cost of operating their Web sites, they should consider reducing the amount of bandwidth usage. Learn how to successfully compress your HTML output and save money on your monthly bandwidth.
    [Read This Article]  [Top]
    Feb 6, 2002 - The Just Two Theory on Web Servers
    Maintaining a large Web farm is both costly and unnecessary. Learn how to reduce your Web farm to just two servers in this controversial article by Wayne Berry.
    [Read This Article]  [Top]
    Aug 14, 2001 - NT Authentication's Impact on Connection Pooling
    Steve Witkop examines OLE DB and ODBC connection pooling when used with Microsoft NT LAN Manager Web server authentication.
    [Read This Article]  [Top]
    Jul 16, 2001 - Removing Duplicates in a String List
    Members of the 15 Seconds discussion list put together a couple of scripts to benchmark methods for removing duplicate items in a string list.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers