25 February 2014

Application state-Server Side State Management

    Application state is used to store data on the application machine. Application variable is an object that is shared by the multiple sessions. We can use application variable within page, Http Handler and Global.asax.

     When request a page from client machine, an instance will create at application machine by the help of Http Application State class. Entire application will have only one instance which is provided by Http Context property named Application.    

Implementation of Application State:

  1) Inside the Global.asax page
   
   void Application_Start(object sender, EventArgs e)
     {

       // Code that runs on application startup

       Application["LoginID"] ="xyz";

       Application["DomainName"] = "www.xyz.com";

      }

  
  2) Inside the .aspx page

   Protected void Page_Load (object sender, EventArgs e)
    {

        // Code that runs on page load

        Application ["LoginID"] = "xyz";

        Application ["DomainName"] = "www.xyz.com";

    }

Application state retrieval:

    string loginID=string.Empty;
    LoginID = Application ["LoginID"].ToString ();

    String loginID = string.Empty;
    LoginID = Application.GetKey (0);

    We can retrieve all application variables on the currently running application's keys by
Using Http application State class.

    HttpApplicationState appState = null;
    appState = Application.Contents;

    String [] StateVars = new String [appState.Count];
    StateVars = appState.AllKeys;


Remove Application State:

   We have three methods to remove application variable from the ASP.NET application.
   Application.Remove("LoginID");
   Application.RemoveAt(0);
   Application.RemoveAll();

Implementing Synchronization in application state

   we can avoid deadlock occurenece while we are updating application variable is used by multiples users with help of lock()  and unlock()
   
      Application.Lock(); 
      Application ["LoginID"] = "xyz";
      Application ["DomainName"] = "www.xyz.com";
      Application.UnLock();

Uses of Application State:   
  •      Application object memory released when we removed.
  •         Multi user can able to access application variable.
  •      To avoid deadlock or conflict we should use Lock and Unlock when we use write or update in the application object.
  •        Other application can't access this application values.
Dis Advantages of Application State: 

  •  Application variable will exists until exit our application.
  •  If we do not have Lock() and Unlock, deadlock will occur.
  •  Its global variable so anyone can access within this application.
  •  Other application can't access this application values.








No comments:

Post a Comment