Chapter 8 part2


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 8 part2

  1. 1. • Session State Configuration• Session Modes in ASP.NET  InProc  StateServer  SQLServer  Custom• Application State• An Overview of State Management Choices
  2. 2. You configure session state through the web.config file.The configuration file allows you to set advanced optionssuch as the timeout and the session state mode.<sessionStatecookieless="UseCookies"cookieName="ASP.NET_SessionId"regenerateExpiredSessionId="false"timeout="20"mode="InProc"stateConnectionString="tcpip="stateNetworkTimeout="10"sqlConnectionString="data source=;Integrated Security=SSPI"sqlCommandTimeout="30"allowCustomSqlDatabase="false"customProvider=""compressionEnabled="false"/>
  3. 3. You can set the cookieless setting to one of the valuesdefined by the HttpCookieMode enumeration,
  4. 4. This specifies the number of minutes that ASP.NET willwait, without receiving a request, before it abandons thesession.You can also programmatically change the sessiontimeout in code. For example, if you know asession contains an unusually large amount ofinformation, you may need to limit the amount of timethe session can be stored.Session.Timeout = 10;
  5. 5. In ASP.NET, there are the following session modesavailable:InProc : It instructs session state information to bestored in the application domainStateServer : With this setting, ASP.NET will use aseparate Windows service called StateServer for statemanagement.SQLServer : This setting instructs ASP.NET to use anSQL Server database to store session informationCustom : This setting use custom or third-party(e.gOracle) provider to store state informationOff : This setting disables session state management
  6. 6. InProc is the default mode. It instructs information tobe stored in the application’s domain, or in the mainASP.NET process, which provides the bestperformance but the least durability.If you restart your server, the state information will belost.In ASP.NET, application domains can be restarted fora variety of reasons, including configuration changesand updated pages, and when certain thresholds aremet.
  7. 7. With this setting, ASP.NET will use a separateASP.NET State Service for state management.This service runs on the same web server, but it’soutside the main ASP.NET process, which gives it abasic level of protection if the ASP.NET process needsto be restarted.When using the StateServer setting, you need tospecify a value for the stateConnectionString.This string identifies the TCP/IP address of thecomputer that is running the StateServer serviceand its port number.For local server it is
  8. 8. Use the Microsoft Management Console (MMC).1. Select Start ➤ Control Panel.2. Open the Administrative Tools group, and then choose Computer Management.3. In the Computer Management tool, go to the Services and Applications ➤ Services node.4. Find ASP.NET State Service in the list5. You can manually start and stop the service by right- clicking it. Select Properties, and modify the Startup Type to Automatic to automatically start the service.When using StateServer mode, you can also set anoptional stateNetworkTimeout attribute that specifies themaximum number of seconds to wait for the service torespond before canceling the request.
  9. 9. This setting instructs ASP.NET to use an SQL Serverdatabase to store session information, as identifiedby the sqlConnectionString attribute.This is the most resilient state store but also theslowest by far.To use this method of state management, you’ll needto have a server with SQL Server installed.In addition, you need to install the special storedprocedures and temporary session databases.These stored procedures take care of storing andretrieving the session information.
  10. 10. ASP.NET includes a command-line tool calledaspnet_regsql.exe to install session storagedatabase on the current computer, and givesdefault database name ASPState.The easiest way to run aspnet_regsql.exe is toopen the Start menu and choose Programs ➤Visual Studio 2010 ➤ Visual Studio Tools ➤ VisualStudio Command Prompt.The command that creates the session storagedatabase ASPState on the current computer is:aspnet_regsql.exe -S localhost -E –ssadd
  11. 11. If you’re using a database named ASPState to store yoursession information (which is the default), you don’t need tosupply the database name. Simply indicate the location of theserver and the type of authentication that ASP.NET should usein the web.config file<sessionState mode="SQLServer"sqlConnectionString="data source=;IntegratedSecurity=SSPI“ ... />When using the SQLServer mode, you can also set an optionalsqlCommandTimeout attribute that specifies the maximumnumber of seconds to wait for the database to respond beforecanceling the request.
  12. 12. When using custom mode, you need to indicate whichsession state store provider to use by supplyingthe customProvider attribute which indicates the name ofthe class.The class may be part of your web application (in whichcase the source code is placed in the App_Codesubfolder), or it can be in an assembly that your webapplication is using (in which case the compiledassembly is placed in the Bin subfolder)Other vendors may release custom state providers youwant to use. For example, Oracle could provide a customstate provider that allows you to store state information inan Oracle database
  13. 13. The enableCompression setting only has an effect whenyou’re using out-of-process session state storage.When you set enableCompression to true, session data iscompressed before it’s passed out of the application domain.There are two key scenarios where session statecompression makes sense:• When the out-of-process state server is hogging a lot of memory• When storing session state data on another computer
  14. 14. Application state allows you to store global objects thatcan be accessed by any client.The command to create Application variable:Application["HitCounterForOrderPage"] = count;// to retrieve an Application variableint count = 0;if (Application["HitCounterForOrderPage"] != null){count = (int)Application["HitCounterForOrderPage"];}
  15. 15. Application state isn’t often used, because it’s generallyinefficient.Because two clients can access an application variableat the same time, you need to use the Application.Lock()and Application.Unlock() methods, which explicitly allowonly one client to access the Application state collectionat a time.
  16. 16. Application state is rarely used in the .NET world because itstwo most common uses have been replaced by easier, moreefficient methods:• In the past, application state was used to store application- wide constants, such as a database connection string which can be stored in the web.config file• Application state can also be used to store frequently used information that is time-consuming to create. It can be replaced more efficiently with caching.