Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET Simplifies State Management in Web Applications : Page 6

If you're tired of writing state management code in your Web applications, you'll be happy to know that ASP.NET not only greatly simplifies the process, but also solves some of the classic ASP Session object's problems, such as using Sessions with Web farms, and persisting state data.


advertisement
Persisting Session Variables to SQLServer
Using a state server lets all the servers in a Web farm share state information; however, that does not prevent state data from being lost if the state server crashes. You can persist state information and keep it accessible (even after a reboot) by storing the state data in a SQL Server database. To do that, modify the web.config file as follows:

<sessionState mode="SQLServer" sqlConnectionString= "data source=127.0.0.1;user id=sa;password=" timeout="20" />

The sqlConnectionString attribute contains the IP address of the machine running SQL Server (127.0.0.1 in this case) as well as the user ID and password required to access the SQL Server.

Next, you need to set up the necessary database and tables to store the state information. Fortunately, ASP.NET provides an installation script to help you setup the database, ASPState, and its tables. To use the script, enter the following at a command prompt (you need to enter the following on a single line):

D:\WINNT\Microsoft.NET\Framework\v1.0.3328> osql -S localhost -U sa -P <installsqlstate.sql

The state information is actually stored in the tempdb database (not ASPState), in the ASPStateTempApplications and ASPStateTempSessions tables. The ASPState database contains several stored procedures that allow ASP.NET to perform housekeeping tasks, such as retrieving session variables and removing expired Session variables. All Session variables would now be stored in SQL Server as shown in Figure 9.

Figure 9: The session variables are stored in two tables—ASPStateTempApplications and ASPStateTempSessions.
To sum up, you've seen an overview of the various methods for maintaining state in ASP.NET. But which of these methods should you choose? Here are some criteria that may help you determine the appropriate state maintenance method for your applications:

  • If security is not a main concern, using Viewstate is a cost-effective way to maintain state. However, the downside of this method is that of increased loading and processing time on the Web browser. Viewstate also exposes the state information on the client side, which may not be desirable if secrecy is a top priority.
  • The Session object offers an effective way to maintain state information on the server-side. However, the Session object should be used sparingly as it increases the workload on the Web server.
  • The Session object supports three modes—InProc, StateServer and SQLServer. With InProc being the least resource intensive, but being the least robust, to SQLServer being the most resource intensive, and the most robust. There is no free lunch; choose the mode carefully to suit your needs.
Remember, maintaining state is a costly affair; enable it only when necessary!



Wei-Meng Lee is a Microsoft .NET MVP and co-founder of Active Developer, a technology company specializing in hands-on training on the latest technologies. He is an established developer and trainer specializing in .NET and wireless technologies. He speaks and writes frequently on topics ranging from .NET to Mac OS X. He is the author of "Windows XP Unwired" (OReilly & Associates) and is currently working on "Programming the .NET Compact Framework," also from OReilly. He can be reached at weimeng@activedevelop.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date