Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Display Request Processing Time in ASP.Net pages
There are some sites in internet who are displaying the time taken to process the request given to the server at the end of every page. This tutorial will help us to implement this feature in our asp.net sites. This also helps a developer to understand how much time it takes to complete the processing of a particular request which he can use for performance check. This tutorial will help us to implement this feature in 2 ways,
  • Using a custom HttpModule.
  • Using Global.asax file.
Even though, we can implement this simple feature in Global.asax file we will also learn this feature through HttpModules. By implementing this simple feature through HttpModules we will understand the basics of HttpModules and building our own custom HttpModules.
What is an HttpModule?
The HttpModules are the components in worker process that helps in completing the request given to the server. HttpModules are similar to ISAPI filters which help the runtime to filter the request given to the server prior to asp.net release. Developers can implement their own filters specific to their application needs. These ISAPI filters implementation can be done through unmanaged code and it is extremely difficult to implement. ISAPI equivalent in ASP.Net is the HttpModules, which are easy to implement. To implement this easily, .Netframework has an interface called IHttpModule which should be utilized with simple config settings.
In ASP.Net, whenever a request is given to the server (http://localhost/WebApp/Default.aspx), there is a mechanism called HttpPipeline which happens in the worker process to complete a request given to the server. HttpPipeline is a mechanism which will receive the request with appropriate information’s through HttpContext object and passes the request to series of object for request completion. During this stage, the involvement of HttpModules comes into picture for servicing the request. There are a number of system-level (asp.net) HTTP modules, providing services like authentication, state management, output caching, etc. We can always write our own HttpModules and configure it for our web application in web.config.

In simple words, writing our own HttpModule will help us to hook a event in every request processing of our application to implement out own application specific needs.

To implement a custom HttpModule, we need to implement IHttpModule interface in System.Web namespace.
public interface IHttpModule
{
     // Summary:
     //         Disposes of the resources (other than memory) used by the module that implements
      //         System.Web.IHttpModule.
     void Dispose();
     //
     // Summary:
     //          Initializes a module and prepares it to handle requests.
      //
     // Parameters:
     //          context:
     //      An System.Web.HttpApplication that provides access to the methods, properties,
     //       and events common to all application objects within an ASP.NET application
     void Init(HttpApplication context);
}
The module that implement the above interface should implement Init() and Dispose() events. A web.Config entry inside <System.Web> is needed to hook this module in the request processing.
<httpModules>
     <add type="namespace.ClassName, namespace" name="ClassName" />
</httpModules>
Using a Custom HttpModule
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace CodeDigestModule
{
     public class PageProcessingTime : IHttpModule
     {        
        DateTime dtStart = DateTime.MinValue;
        public PageProcessingTime()
        {
           //
          // TODO: Add constructor logic here
          //
        }
       private HttpApplication mApplication;
       public void Init(System.Web.HttpApplication application)
       {
         // Wire up beginrequest
         application.BeginRequest += new System.EventHandler(BeginRequest);
         // Wire up endrequest
         application.EndRequest += new System.EventHandler(EndRequest);
         // Save the application
         mApplication = application;
       }
       public void BeginRequest(object sender, EventArgs e)
       {
            dtStart = DateTime.Now;   
       }
       public void EndRequest(object sender, EventArgs e)
       {
           DateTime dtEnd = DateTime.Now;
           TimeSpan tsProcessingTime = dtEnd - dtStart;
          mApplication.Response.Write("<b>Request Processing Time: Using HttpMoules </b>" + tsProcessingTime.ToString());
       }
       public void Dispose()
      {
      }
    }
}
Web.Config
<httpModules>
     <add type="CodeDigestModule.PageProcessingTime, CodeDigestModule" name="PageProcessingTime" />
</httpModules>
When we execute the project, we will get an output with the processing time at the end of the every page.
Using Global.asax file
By default, there will be no Global.asax file added to your solution when using Visual Studio 2005. We need add it explicitly through “Add New Item” dialog of Visual Studio 2005.
<%@ Application Language="C#" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
          // Code that runs on application startup
    }
    void Application_End(object sender, EventArgs e)
    {
          //  Code that runs on application shutdown
    }
   void Application_Error(object sender, EventArgs e)
   {
         // Code that runs when an unhandled error occurs
   }
   void Session_Start(object sender, EventArgs e)
   {
         // Code that runs when a new session is started
   }
   void Session_End(object sender, EventArgs e)
   {
         // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
      // is set to InProc in the Web.config file. If session mode is set to StateServer
       // or SQLServer, the event is not raised.
   }
   protected void Application_BeginRequest(Object sender, EventArgs e)
   {       
       Context.Items.Add("Request_Start_Time", DateTime.Now);
   }
    protected void Application_EndRequest(Object sender, EventArgs e)
   {
      TimeSpan tsDuration=
DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);      Context.Response.Write("<b>Request Processing Time: From Global.asax file " + tsDuration.ToString());>
    }
</script>
When we execute the project, we will get an output with the processing time at the end of the every page.
Job or extra money for students

Search Engine Rank of your blog or websites