Saturday, February 11, 2012

session and application in asp.net


Asp.net is a collection of objects.In that application is one.In all asp.net objects "application" is one asp.net object.In order to serve any resource from a web app first application should be loaded  and then resource will be serve.
Application:
When first user makes a request for any resource in the app'n then asp.net creates an app domain and loads the project inside it.When all sessions at server are lost and allocated application resources are also destroyed then run time can remove the appn.

session:
when  a new user makes a request  session is used to uniquely identify uses in a a web app.session is created  whenever a new user  makes a request .Every session is Identified with a unique id called session Id.This id travels between requests and response
Every session create with a timeout 20 min by default
if user doesn't respond with in 20 minits then sever is going to all cleanup the session.alter this if user makes a request creates a new session and serves data in the session will be last
Session can also be closed pragmatically using Abandon() method of it

Session.Abandon();a=0;
Session.Abandon();
b=10;
Response.Write(b.Tostring());
Developer can use asp.net application that represents an application and session which represents the sub process or session only in 2 ways
1.In the form variables for state management
2.In the form of events preforming actions and during their involved tasks.
Both variables and events are not mandatory for any web app but every project needs application and session level takes which are fulfilled only using events and also secured,higher level state provided with application and session variables only
public void application_Onstart()
{
Application["users]=0;
Application["usersonline]=0;
}
public void session_Onstart()
{
Application.Lock();
Application["user"]=(int)Applciation ["Useronline"]+1;
Application.Unlock();
}
public void session_OnEnd()
{
Application.Lock();
Application["useronline"]=(int)Applciation ["Useronline"]-1;
Application.Unlock();
}
//under page load
{
if(!Ispostback)
session.TimeOut=1;
}
protected void button _click(object sender,Event args e)
{
lblapp.Text="visitorno:"+Applcation["users"].To string();
lbl.Text="onlineno:"+Applcation["usersonline"].To string();
}
//In markup language
go  to web.config
<system.web>
<session state timeout="1"></session state>

we can change the time of half session pragmatically using  session.Timeout=n' and also in web.config markup using  session.timeout=1
By default session are also maintained internally using cookies which means we are dependent on browser again and our application may not run based on browser settings.Again session i asp.net can be managed cookies less.Cookie less means we are dependent on URI and using session state tag we can specify this settings

Post a Comment

Bel