Home > Uncategorized > ASP.NET–Asynchronous HTTP Module

ASP.NET–Asynchronous HTTP Module

Hi Friends,

An HTTP module is an object that sits in the ASP.NET pipeline, where it can see-and even modify-incoming requests and outgoing responses.

Today while working with website containing multiple applications , I faced a scenario where i need to  log incoming requests in database . By collecting the login name, a date and time, and the application that the user accessed during the login session.

By thinking on above scenario one would say what’s a big deal in this!! we can make entry every time the user clicks on the application .but problem with this solution is it would give call to the database every time user clicks on any application. First, it performs database insert. Second, it uses a request-processing thread to perform the I/O-a thread that could otherwise be used to service additional incoming requests, which imposes a penalty on throughput.

So for this we decided to write a http module which made it possible for us, but Question is How httpModule improve our performance / throughput the answer is ASYNCHRONOUS HttpModule.

Asynchronous Http Module
  1. public class AsyncModule : IHttpModule
  2.     {
  3.         /// <summary>
  4.         /// You will need to configure this module in the web.config file of your
  5.         /// web and register it with IIS before being able to use it. For more information
  6.         /// see the following link: http://go.microsoft.com/?linkid=8101007
  7.         /// </summary>
  8.         #region IHttpModule Members
  10.         string conString = "Data Source=localhost;initial catalog=OpenLDAP;integrated security=true;Asynchronous Processing=true";
  11.         SqlConnection con;
  12.         SqlCommand cmd;
  15.         public void Dispose()
  16.         {
  17.             if (con != null)
  18.                 con.Close();
  19.         }
  21.         public void Init(HttpApplication context)
  22.         {
  23.             context.AddOnPreRequestHandlerExecuteAsync(new BeginEventHandler(OnBeginAsync), new EndEventHandler(OnEndAsync));
  24.         }
  26.         #endregion
  28.         public void OnLogRequest(Object source, EventArgs e)
  29.         {
  30.             //custom logging logic can go here
  31.         }
  32.         IAsyncResult OnBeginAsync(Object source, EventArgs e, AsyncCallback cb, Object state)
  33.         {
  35.             //Write your logic here to track the Request//
  36.             con = new SqlConnection(conString);
  37.             con.Open();
  38.             cmd = new SqlCommand();
  39.             cmd.Connection = con;
  40.             cmd.CommandText = "Insert into documents values('abstract3','title',null)";
  41.             return cmd.BeginExecuteNonQuery(cb, state);
  43.         }
  44.         public void OnEndAsync(IAsyncResult result)
  45.         {
  46.             cmd.EndExecuteNonQuery(result);
  48.         }
  50.     }

How above module works asynchronous???

Its Init method calls HttpApplication.AddOnPreRequestHandlerExecuteAsync in order to register begin and end methods for PreRequestHandlerExecute events.

The HttpApplication class features other AddOn methods for other per-request events. For example, an HTTP module can call AddOnBeginRequestAsync to register async handlers for BeginRequest events.

AsyncRequestLogModule’s OnBeginAsync method uses the framework’s BeginExecuteNonQuery method to begin an asynchronous insert. The moment OnBeginAsync returns, the thread goes back into the thread pool.

Thank You,

Hope this helps Smile

Categories: Uncategorized
  1. D2
    June 14, 2012 at 5:09 am

    Great article. I’m pretty new to the whole async ops in ASP.NET. Could you explain to me how this is used in an aspx page. Is it automatically called by IIS?

  2. June 14, 2012 at 10:58 am

    Yes it is called by IIS internally.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: