Saturday, April 8, 2017

Web Application Life Cycle Event ASP.NET

Events in the life cycle of a web application:
Today we will discuss the events in the life cycle of a web application. As the name shows Application events are used across the application. these events are used to initialize some data which will be available to all current user of application. As we know Session are also used to initialize the data but that data will be available for individual session not for whole application. First let's have a look on Web application events available in Global.asax file:
void Application_Start(object sender, EventArgs e)
{
    //This Code will run on application start
}
void Application_End(object sender, EventArgs e)
{
    //This Code will run on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
    //This Code will run when some unhandled error occured
}
void Session_Start(object sender, EventArgs e)
{
        //This Code will run when a new session starts
}
void Session_End(object sender, EventArgs e)
{
    //This Code will run on when a session ends
} 


1. Application_Start: This event runs when an application starts running first time if it was not running before. mostly this even is used to initialize data that needs to be available across all the application. we can set an application state variable to keep records that when an application starts and we can also count of application start event fired.
void Application_Start(object sender, EventArgs e)
{
    // Create Application state variables
    Application["AppStartDateTime"] = DateTime.Now;
}

This will available across the application if required on some pages.

2. Application_End: This event will be fired when application shutdown. we can use this event to keep records and also, we can generate some alert so we could be aware of application is running or shutdown. this event will be fired when IIS is restarted basically it is a once per application event.
void Application_End(object sender, EventArgs e) 
{
    //Code
}


3. Application_Error: This event is mostly used than above mentioned events because when ever and error occurred then this event will be fired and we can set redirect according to error status code so our used should be face some terrible error page and we present some user-friendly message to use and mention the reason why user come to this page.
void Application_Error(object sender, EventArgs e) 
    { 
        // Code runs when an unhandled error occurs
    Exception ex = Server.GetLastError();
    if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
    {
        Response.Redirect("~/404.aspx");
        //Server.Transfer("~/Error.aspx");
    }
    else
    {
        // your global error handling here!
    }
}


4. Session_Start: This event will be fired every time when a new user/browser visits the application with different session id. by using this Event we can keep record how many user visits our application and we can check how many users currently on our application.
void Session_Start(object sender, EventArgs e)
{
    //Application["TotalUser"],Application["CurrentUser"] will be initialize in application_start
    // Increment TotalUserSessions by 1
    Application["TotalUser"] = (int)Application["TotalUser"] + 1;
    Application["CurrentUser"] = (int)Application["CurrentUser"] + 1;
}


5. Session_End: This event is fired when a single user's session ends or timed out.
void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends.  
    Application["CurrentUser"] = (int)Application["CurrentUser"] - 1;   
    }
Note: Session End event is fired only when we have set session state mode to "InProc". In other state mode, it will not fire Session End event.

Comments and Suggestions are Always Welcome!

No comments:

Post a Comment