Thursday, August 25, 2011

Types of Sessions in ASP.Net


There are two main categories of session in asp.net2.0,
(1) Session InProcess:
        InProc
(This is default in web.cofig file and high performence.)

(2) Session OutProcess:
      State Server
      Sql Server
(These out proc is useful whethr the infrmation is
maintained in between to servers that time only we can use
this sessions.)

Simply three types of session in asp.net2.0,
i)InProc
ii)State Server
iii)SQL Server


There are two category of Session type:

(1) Inproc : are those which are stored under aspnet_wp.
(2) OutProc : are those which are stored outside aspnet_wp.

- Inproc : This is default session state and it is stored
locally in the memory of asp net worker process.

- OutProc :
      (a) StateServer : it is stored outside aspnet_wp & is
managed by window service. Location of that service is
specified by stateConnectionString attribute.
      (b) SqlServer : it is also stored outside aspnet_wp &
is managed by Sql Server Database. Location of that database
is specified by sqlConnectionString attribute.


ASP.Net supports 4 types of sessions
1)in process (InProc)(The Default) Session state exists
within the process the web is using
2)state server (OutProc)Session data is sent to the
configured stateserver service
3)sql server (OutProc)Session data is store in the
configured sql server database
4)custom

InProc
Adv.:
1) Faster as session resides in the same process as the
application
2) No need to serialize the data

DisAdv.:
1) Will degrade the performance of the application if large
chunk of data is stored
2) On restart of IIS all the Session info will be lost

State Server
Adv.:
1) Faster then SQL Server session management
2) Safer then InProc. As IIS restart
won't effect the session data

DisAdv.:
1) Data need to be serialized
2) On restart of ASP.NET State Service, session info will be lost
3)Slower as compared to InProc

SQL Server
Adv.:
1) Reliable and Durable
2) IIS and ASP.NET State Service
restart won't effect the session data
3) Good place for storing large chunk of data

DisAdv.:
1) Data need to be serialized
2) Slower as compare to InProc and State Server
3)Need to purchase Licensed
version of SQL Server

By default, ASP.NET will store the session state in the same process that processes the request, just as ASP does. Additionally, ASP.NET can store session data in an external process, which can even reside on another machine. To enable this feature:

Start the ASP.NET state service, either using the Services snap-in or by executing "net start aspnet_state" on the command line. The state service will by default listen on port 42424. To change the port, modify the registry key for the service: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\Port
Set the mode attribute of the section to "StateServer".
Configure the stateConnectionString attribute with the values of the machine on which you started aspnet_state.

The following sample assumes that the state service is running on the same machine as the Web server ("localhost") and uses the default port (42424):

Note that if you try the sample above with this setting, you can reset the Web server (enter iisreset on the command line) and the session state value will persist.



State Management in .Net
How many ways are there to maintain a state in .Net? What is view state?
Web pages are recreated each time the page is posted to the server. In traditional Web programming, this would ordinarily mean that all information associated with the page and the controls on the page would be lost with each round trip.
To overcome this inherent limitation of traditional Web programming, the ASP.NET page framework includes various options to help you preserve changes — that is, for managing state. The page framework includes a facility called view state that automatically preserves property values of the page and all the controls on it between round trips.
However, you will probably also have application-specific values that you want to preserve. To do so, you can use one of the state management options.

Client-Based State Management Options:
  •  View State
  • Hidden Form Fields
  • Cookies
  • Query Strings

Server-Based State Management Options
  • Application State
  • Session State
  • Database Support

What are the disadvantages of view state / what are the benefits?
Automatic view-state management is a feature of server controls that enables them to repopulate their property values on a round trip (without you having to write any code). This feature does impact performance, however, since a server control's view state is passed to and from the server in a hidden form field. You should be aware of when view state helps you and when it hinders your page's performance.
When maintaining session through Sql server, what is the impact of Read and Write operation on Session objects? will performance degrade?
Maintaining state using database technology is a common practice when storing user-specific information where the information store is large. Database storage is particularly useful for maintaining long-term state or state that must be preserved even if the server must be restarted.

No comments:

Post a Comment