Professional Documents
Culture Documents
Hands-On Lab
Hands-On Lab
Lab Manual
DEV-HOL19: Introduction to ASP.NET caching,
tracing and state management
Information in this document is subject to change without notice. The example companies,
organizations, products, people, and events depicted herein are fictitious. No association with
any real company, organization, product, person or event is intended or should be inferred.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the
rights under copyright, no part of this document may be reproduced, stored in or introduced into
a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written
permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL
Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++,
Visual InterDev, Visual SourceSafe, Visual C#, Visual J#, and Visual Studio are either
registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other
countries.
Other product and company names herein may be the trademarks of their respective owners.
2
This lab provides a simple introduction to some of the great new features of ASP.NET simplify web
application development.
Objectives
After completing this lab, you will be able to:
C:\HandsOnLabs\ASP.NET\VB
Exercises
Exercise 1: ASP.NET Session State
ASP.NET introduces several new ways to store session state which allow you to build more robust and
fault tolerant applications than ever before. This lab provides an overview of the three state management
options offered by ASP.NET.
3
Exercise #1: ASP.NET Session State
In this exercise, you will create a new ASP.NET web application in Visual Studio .NET, and experiement
with some different session state configuration options, and learn about cookieless session state.
4
Before testing, you need to start the ASP.NET State Service (which will store the session state data).
Click Start->Run and type net start ASPNET_STATE.
Press Ctrl+F5 to compile and run the page.
Refresh the page a few times by clicking Refresh in the Web browser. Wait a few seconds between
each refresh. Note that the time of the last access is stored in the session state object between
refreshes.
Restart the web server. Click Start->Run and type iisreset.
Click OK.
Now go back to the Web browser and click Refresh again.
Note that the ”Last accessed” time is still available. We have no lost our session state data.
Close the Web browser
Note 1: If you wish to share state between different web servers (in a web farm scenario for instance), you can
run the ASP.NET State Service on a remote machine. Your application can be directed to this remote
machine using the StateConnectionString attribute of the <SessionState> tag in web.config. Simply
set the value of StateConnectionString to the IP address of the machine running the ASP.NET State
Service.
Note 2: You may also store state information in a SQLServer database. To do this change the mode attribute
of the <SessionState> tag to “SQLServer”.
Note; You may be asking when cookieless sessions are applicable. One case is when a corporate policy
prevent usage of cookies, any Intranet applications then need to use a different way to associate a client
with data on the server. There are alternatives to using session data, e.g. hidden fields containing data,
but nothing is as easy to use as normal session data.
5
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>
At the top your page will be displayed. Below your page is a number of informational elements that is
useful for understanding how the page was created and which settings were available at the time.
After having looked at the basic trace information let’s insert some trace statements of your own. The
Trace class statement comes in two flavors: Write and Warn. They both take the same arguments. The
only difference between them is the color they trace in, Write uses black and Warn uses red.
6
Press Ctrl+F5 to compile the application. Observe the resulting trace information on the page. Each
event is shown as well as the execution time between each event.
Close the web browser.
7
This is the simplest for of caching. As many pages have slightly different contents depending on a
simple parameter in the query string this simple form of caching can be extended. You will now utilize
the VaryByParam argument in the OutputCache directive above.
Note: ASP.NET provides several very flexbile and easy to use caching options which makes it much simpler
for you to build high performance applications. We did not highlight it here, but ASP.NET also allows
you to cache fragments of pages, instead of the entire page. For more information on page fragment
caching please refer to the PAG lab series (also on this machine) or MSDN (msdn.microsoft.com).