- •Contents
- •Introduction
- •Acknowledgments
- •The Goals of ASP.NET 2.0
- •Developer productivity
- •Administration and management
- •Performance and scalability
- •Device-specific code generation
- •Additional New Features of ASP.NET 2.0
- •New developer infrastructures
- •New compilation system
- •Additions to the page framework
- •New objects for accessing data
- •New server controls
- •A New IDE for Building ASP.NET 2.0 Pages
- •The Document Window
- •Views in the Document Window
- •The tag navigator
- •Page tabs
- •Code change status notifications
- •Error notifications and assistance
- •The Toolbox
- •The Solution Explorer
- •Lost Windows
- •Other Common Visual Studio Activities
- •Creating new projects
- •Making references to other objects
- •Using smart tags
- •Saving and importing Visual Studio settings
- •Application Location Options
- •Built-in Web server
- •Web site requiring FrontPage Extensions
- •The ASP.NET Page Structure Options
- •Inline coding
- •New code-behind model
- •New Page Directives
- •New attributes
- •New directives
- •New Page Events
- •Cross-Page Posting
- •New Application Folders
- •\Code folder
- •\Themes folder
- •\Resources folder
- •Compilation
- •The New Data Source Controls
- •The SqlDataSource and GridView Controls
- •Reading data
- •Applying paging in the GridView
- •Sorting rows in the GridView control
- •Defining bound columns in the GridView control
- •Enabling the editing of rows in the GridView control
- •Deleting data from the GridView
- •Dealing with other column types in the GridView
- •Selecting which fields to display in the DetailsView control
- •Using the GridView and DetailsView together
- •Updating, inserting, and deleting rows
- •XmlDataSource Control
- •ObjectDataSource Control
- •SiteMapDataSource Control
- •DataSetDataSource Control
- •Visual Studio 2005
- •Connection Strings
- •Site Maps
- •The PathSeparator property
- •The PathDirection property
- •The ParentLevelsDisplayed property
- •The ShowToolTips property
- •Examining the parts of the TreeView control
- •Binding the TreeView control to an XML file
- •Selecting multiple options in a TreeView
- •Specifying custom icons in the TreeView control
- •Specifying lines used to connect nodes
- •Working with the TreeView control programmatically
- •Applying different styles to the Menu control
- •Menu Events
- •Binding the Menu control to an XML file
- •SiteMap Data Provider
- •SiteMapViewType
- •StartingNodeType
- •SiteMap API
- •Why Do You Need Master Pages?
- •The Basics of Master Pages
- •Coding a Master Page
- •Coding a Content Page
- •Mixing page types and languages
- •Specifying which master page to use
- •Working with the page title
- •Working with controls and properties from the master page
- •Nesting Master Pages
- •Container-Specific Master Pages
- •Event Ordering
- •Caching with Master Pages
- •Using ASP.NET 2.0 Packaged Themes
- •Applying a theme to a single ASP.NET page
- •Applying a theme to an entire application
- •Applying a theme to all applications on a server
- •Removing themes from server controls
- •Removing themes from Web pages
- •Removing themes from applications
- •Creating Your Own Themes
- •Creating the proper folder structure
- •Creating a skin
- •Including CSS files in your themes
- •Having your themes include images
- •Defining Multiple Skin Options
- •Programmatically Working with Themes
- •Themes and Custom Controls
- •Authentication
- •Authorization
- •ASP.NET 2.0 Authentication
- •Setting up your Web site for membership
- •Adding users
- •Asking for credentials
- •Working with authenticated users
- •Showing the number of users online
- •Dealing with passwords
- •ASP.NET 2.0 Authorization
- •Using the LoginView server control
- •Setting up your Web site for role management
- •Adding and retrieving application roles
- •Deleting roles
- •Adding users to roles
- •Getting all the users of a particular role
- •Getting all the roles of a particular user
- •Removing users from roles
- •Checking users in roles
- •Using the Web Site Administration Tool
- •The Personalization Model
- •Adding a simple personalization property
- •Using personalization properties
- •Adding a group of personalization properties
- •Using grouped personalization properties
- •Defining types for personalization properties
- •Using custom types
- •Providing default values
- •Making personalization properties read-only
- •Anonymous Personalization
- •Enabling anonymous identification of the end user
- •Working with anonymous identification events
- •Anonymous options for personalization properties
- •Migrating Anonymous Users
- •Personalization Providers
- •Working with the Access personalization provider
- •Working with the SQL Server personalization provider
- •Using multiple providers
- •Building Dynamic and Modular Web Sites
- •Introducing the WebPartManager control
- •Working with zone layouts
- •Understanding the WebPartZone control
- •Explaining the WebPartPageMenu control
- •Modifying zones
- •Caching in ASP.NET 1.0/1.1
- •Output caching
- •Partial page caching
- •Data caching using the Cache object
- •Cache dependencies
- •ASP.NET 2.0 unseals the CacheDependency class
- •Enabling databases for SQL Server cache invalidation
- •Enabling tables for SQL Server cache invalidation
- •Looking at SQL Server
- •Looking at the tables that are enabled
- •Disabling a table for SQL Server cache invalidation
- •Disabling a database for SQL Server cache invalidation
- •Configuring your ASP.NET Application
- •Adding more than one table to a page
- •Attaching SQL Server cache dependencies to the Request object
- •Attaching SQL Server cache dependencies to the Cache object
- •Customizing the side navigation
- •Examining the AllowReturn attribute
- •Working with the StepType attribute
- •Adding a header to the Wizard control
- •Utilizing Wizard control events
- •Working with images from disk
- •Resizing images
- •Displaying images from streams
- •The MMC ASP.NET Snap-In
- •General
- •Custom Errors
- •Authorization
- •Authentication
- •Application
- •State Management
- •Advanced
- •ASP.NET Web Site Administration Tool
- •Home
- •Security
- •Profile
- •Application
- •Provider
- •Managing the Site Counter System
- •Generics
- •Iterators
- •Anonymous Methods
- •Operator Overloading
- •Visual Basic XML Documentation
- •New Visual Basic Keywords
- •Continue
- •Using
- •Global
- •Index
SQL Cache Invalidation
Configuring your ASP.NET Application
After you enable a database for SQL Server cache invalidation, and you also enable a couple of tables within this database, the next step is to configure your application for SQL Server cache invalidation.
To configure your application to work with SQL Server cache invalidation, the first step is to make some changes to the web.config file. In the web.config file, specify that you want to work with the Northwind database and you want ASP.NET connected to it.
Listing 11-1 shows an example of how you should change your web.config file in order to work with SQL Server cache invalidation.
Listing 11-1: Configuring the web.config file
<configuration>
<connectionStrings>
<add name=”AppConnectionString1” connectionString=”Provider=SQLOLEDB.1; Data Source=EVJEN01;User ID=sa;Password=;Initial Catalog=Northwind;Persist Security Info=False” providerName=”System.Data.OleDb” />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled=”true”> <databases>
<add name=”Northwind” connectionStringName=”AppConnectionString1” pollTime=”500” />
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
From this listing, you can see that the first thing established is the connection string to the Northwind database using the <connectionStrings> element in the web.config file. It is important to make note of the name of the connection string because it is utilized later in the configuration settings for SQL Server cache invalidation.
The SQL Server cache invalidation is configured using the new <caching> element. This element must be nested within the <system.web> elements. Because you are working with a SQL Server cache dependency, you must use a <sqlCacheDependency> child node, and you enable the entire process by using the enabled=”true” attribute. After this attribute is enabled, you work with the <databases> section. Nested within the <databases> nodes, you use the <add> element to reference the Northwind database. The following table explains all the attributes of the <add> element.
331
Chapter 11
Attribute |
Description |
|
|
name |
The name attribute provides an identifier to the SQL Server database. |
connectionStringName |
The connectionStringName attribute specifies the name of the |
|
connection. Because the connection string in the preceding example |
|
is called AppConnectionString1, you use this value for the con- |
|
nectionStringName attribute as well. |
pollTime |
The pollTime attribute specifies the time interval from one SQL |
|
Server poll to the next. The default is 5 seconds or 500 milliseconds |
|
(as shown in the previous example). |
|
|
Now that the web.config file is set up correctly, you can start using SQL Server cache invalidation on your pages. ASP.NET makes a separate SQL Server request on a completely different thread to the
AspNet_SqlCacheTablesForChangeNotification table to see if the changeId number has been incremented. If the number is changed, ASP.NET knows that an underlying change has been made to the SQL Server table and that a new result set should be retrieved. When it checks to see if it should make a SQL Server call, the request to the small AspNet_SqlCacheTablesForChangeNotification table has a single result. This is done in such a quick fashion that you can notice the difference in speed with SQL Server cache invalidation enabled.
Testing SQL Ser ver Cache Invalidation
Now that the web.config file is set up and ready to go, the next step is to actually apply these new capabilities to a page. For an example of a page using the new SQL Server cache invalidation process, look at Listing 11-2.
Listing 11-2: An ASP.NET page utilizing SQL Server cache invalidation
VB
<%@ Page Language=”VB” %>
<%@ OutputCache Duration=”3600” VaryByParam=”none” SqlDependency=”Northwind:Customers”%>
<script runat=”server”>
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = “Page created on “ & DateTime.Now.ToString()
End Sub </script>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>Sql Cache Invalidation</title> </head>
<body>
<form id=”form1” runat=”server”>
<asp:Label ID=”Label1” Runat=”server”></asp:Label><br /> <br />
<asp:GridView ID=”GridView1” Runat=”server” DataSourceID=”SqlDataSource1”> </asp:GridView>
332
SQL Cache Invalidation
<asp:SqlDataSource ID=”SqlDataSource1” Runat=”server” SelectCommand=”Select * From Customers”
ConnectionString=”<%$ ConnectionStrings:AppConnectionString1 %>” ProviderName=”<%$ ConnectionStrings:AppConnectionString1.providername %>”>
</asp:SqlDataSource>
</form>
</body>
</html>
C#
<%@ Page Language=”C#” %>
<%@ OutputCache Duration=”3600” VaryByParam=”none” SqlDependency=”Northwind:Customers”%>
<script runat=”server”>
void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = “Page created on “ + DateTime.Now.ToString();
}
</script>
The first and most important part of this page is the OuputCache page directive that is specified at the top of the file. Typically, the OutputCache directive specifies how long the page output is held in the cache using the Duration attribute. It is followed by the VaryByParam attribute, which does not permit separate page outputs to be cached based on factors like the requestor’s browser. The new addition is the SqlDependency attribute. This enables a particular page to use SQL Server cache invalidation. The following line shows the format of the value for the SqlDependency attribute:
SqlDependency=”database:table”
The value of Northwind:Customers specifies that you want the SQL Server cache invalidation enabled for the Customers table within the Northwind database. The Duration attribute of the OutputCache directive shows you that, typically, the output of this page is stored in the cache for a long time — but this cache is disabled if the Customers table has any underlying changes made to the data that it contains.
A change to the any of the cells in the Customers table of the Northwind database invalidates the cache, and a new cache is generated from the results, which now contain a new SQL Server database request. Figure 11-9 shows an example of the page generated the first time it is run.
Figure 11-9
333
Chapter 11
From this figure, you can see the contents of the customer with the CustomerID of ALFKI. For this entry, you go to SQL Server and change the value of the ContactName from Maria Anders to Mary Anders. Before SQL Server cache invalidation, this change would have done nothing to the output cache. The original page output in the cache would still be present and the end user would still see the Maria Anders entry for the duration specified in the page’s OutputCache directive. Because of SQL Server cache invalidation, after the underlying information in the table is changed, the output cache is invalidated, a new result set is retrieved, and the new result set is cached. When a change has been made, you see the results shown in Figure 11-10.
Figure 11-10
Adding more than one table to a page
The preceding example shows how to use SQL Server cache invalidation for a single table on the ASP.NET page. What do you do if your page is working with two or more tables?
To add more than one table, you use the OutputCache directive shown here:
SqlDependency=”database:table;database:table”
From this example, you can see that the value of the SqlDependency attribute separates the databases and tables with a semicolon. If you want to work with both the Customers table and the Products table of the Northwind database, you construct the value of the SqlDependency attribute as follows:
SqlDependency=”Northwind:Customers;Northwind:Products”
Attaching SQL Server cache dependencies to the Request object
In addition to changing settings in the OutputCache directive to activate SQL Server cache invalidation, you can also set the SQL Server cache invalidation programmatically. To do so, you use the SqlCache Dependency class, which is illustrated in Listing 11-3.
334
