Multiple Approaches to State Management - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Multiple Approaches to State Management

Description:

Remember that once the server generates the HTML for a single page and sends it ... Not devoting extensive resources (memory, processor time, etc.) to managing ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 23
Provided by: bai91
Category:

less

Transcript and Presenter's Notes

Title: Multiple Approaches to State Management


1
Managing State
  • Multiple Approaches to State Management

2
HTTP, Web are Stateless
  • Remember that once the server generates the HTML
    for a single page and sends it to the client to
    render, the server-side app ends leaving no
    program running behind the page.
  • Thus, nothing is running to save the state of
    the application. State describes the set of
    values of variables, properties, and other
    entities that the program uses to manage its
    functionality.

3
Web is Stateless
  • The stateless nature of the web is intentional in
    a sense. Web servers are intended to be scalable
    to be able to serve requests for large numbers
    of users without significant performance
    degradation
  • Not devoting extensive resources (memory,
    processor time, etc.) to managing state for all
    client allows for greater scalability. A web
    server may have thousands of clients at a time.

4
Managing State in a Stateless World
  • It is up to the developer with help from
    ASP.NET - to preserve whatever state is
    essential.
  • Web Form controls have built-in state management.
    The state of a control is preserved over a
    post-back to the same Page.
  • ASP.NET supports four different approaches for
    the developer to manage other types of state such
    as the value of a counter or name of a file.

5
State Management in ASP.NET
  • ASP.NET supports 4 approaches to state management
  • ViewState Page maintains state of its .NET
    controls not useful for cross-page state
  • SessionState individual user information
    relating to entire session
  • ApplicationState useful for read-only global
    application settings. Typically better to use
    configuration files or cache instead.
  • Cookies maintained on client machine by client
    browser may be transient or permanent

6
Application State
  • Application State is designed to be used to
    manage state that applies to the entire
    application across all of the users of the
    application.
  • Since there could be thousands of users, saving
    and retrieving application state can be resource
    intensive.
  • It is also complex to ensure that the state is
    valid with so many users updating it.
  • May be initialized in the Application_Start event
  • Must use Application.Lock/Unlock when changing
    state (changing Application State is strongly
    discouraged)
  • Dangerous because many clients may be accessing
    same item at one time.
  • Inefficient and care required to prevent
    corrupted data.
  • Application State is not maintained across
    machines/processors in web farm or web garden.
  • Better to use cache than Application State.

7
Session State
  • Session state is used to maintain per user
    information across a session
  • The session state is maintained with an ID that
    is difficult to guess.
  • The session ID is passed via cookies by default.
  • Can be accessed via the Session property of each
    Page.
  • Can be initialized in Session_Start event handler
    if appropriate.

8
Using Session State
  • The Session State is maintained as a bag (a
    collection object) of ordered pairs.
  • One item of each pair is the name assigned to
    the value.
  • The other item is the value associated with the
    name.
  • Syntax is
  • Session (Name) value
  • Since each value in the Session State collection
    is of type System.Object, one must cast the value
    to the appropriate type when it is retrieved
    before it can be used.
  • nCount CType (Session (Counter), Integer)

9
Session State in Code
10
Session State in ASP.NET
  • Can be configured to be maintained across
    multiple server machines in a web farm.
  • Can be maintained across application
    shutdown/startup.
  • Session ID is managed with cookies or without
    cookies based on configuration set in web.config
    in the sessionState section.

11
Session State Options
12
Sharing Session ID
  • The ID of the session can either be passed from
    client to server and back to the server using
  • a cookie
  • URL mangling

13
Using URL Mangling to Pass Session Key
14
Session Key Management Comparisons
  • Cookies are slightly more efficient because they
    avoid some round trips to the server.
  • Cookies may not be supported by all clients.
  • Mangled URLs work with all clients
  • Mangled URLs only work with relative URLs
    rather than absolute URLs.
  • An absolute URL is a fully qualified URL
    including the protocol (http, ftp, etc.) and
    all segments of the address starting with the
    server name or IP address
  • A relative URL gives only the part of the address
    necessary relative to the current virtual
    directory (e.g. default.aspx) or the root
    directory of the application (e.g.,
    /app/subapp/default.aspx) for example.

15
Out-of-Process Session State
  • It is possible to maintain the session state
    somewhere other than in memory within ASP.NET.
  • In a system service (the ASP.NET State Server
    service) designed for the purpose
  • In a SQL Server database
  • In a byte stream
  • Requires a round-trip to another server if not
    stored locally.

16
Out of Process Storage DB Server
17
Using Cookies
  • Cookie is a small file stored on the client
    machine by the client browser
  • Server requests client browser to set a cookie
    with a specific value in a response from the
    server to client
  • Can be used to store preferences or user-specific
    information
  • Client sends cookie info in subsequent requests
  • Cookies may be set to expire in a given time
    period (say 5 minutes) or may be persistent for
    permanent record
  • Cookies are small only 4096 bytes guaranteed by
    standards, so amount of data is limited
  • Clients may disable cookies or prohibit
    persistent cookies.

18
Example Using a Cookie in ASP.NET
19
ViewState
  • ViewState is a hidden field on each Page that
    stores page-specific information for a client
    across Post-Backs to same Page
  • Data is not preserved between Pages
  • Types in ViewState must be serializable

20
ViewState
  • ViewState is similar to Session State in that it
    is a collection of name-value pairs, and the
    syntax of using it is similar to that for using
    Session State.
  • ViewState hidden field is base64 encoded to
    minimize memory requirements.

21
ViewState Example
  • Private nCount As Integer 0
  • Private Sub Page_Load (ByVal sender As
    System.Object, _
  • ByVal e As System.EventArgs) Handles
    MyBase.Load
  • Me.lblHello.ForeColor System.Drawing.Color.Fro
    mArgb (rRan.Next)
  • If Me.IsPostBack Then
  • nCount CType (Me.ViewState ("Counter"),
    Integer)
  • End If
  • End Sub
  • Private Sub btnColor_Click (ByVal sender As
    System.Object, _
  • ByVal e As System.EventArgs) Handles
    btnColor.Click
  • nCount 1
  • Me.lblClickCounter.Text _
  • String.Format ("The 'Click Me' button has been
    clicked 0 times.", nCount)

22
Comparison of State Types
Write a Comment
User Comments (0)
About PowerShow.com