How to: Configure the AppFabric Output Cache Provider for ASP.NET (AppFabric 1.1 Caching)
This topic explains how to configure an ASP.NET web application to use a provisioned AppFabric cache for output caching. The configuration is done by modifying the web.config file for the target web application.
Using Microsoft AppFabric 1.1 for Windows Server Caching for Output Caching
For your ASP.NET Web application to use the AppFabric output cache provider, you must add the following elements to your application's web.config file:
configSections
: This element must be the first element in the configuration file, under the openingconfiguration
tag. It is required for the AppFabric Caching assemblies to function.dataCacheClients
: This element is a child of the configuration element. It is used to holddataCacheClient
elements that configure cache clients and specify the cache hosts.caching
: This element is a child of thesystem.web
element. It contains anoutputCache
element that specifies to the Web application that it should use Microsoft AppFabric 1.1 for Windows Server to manage output cache data. ThecacheName
attribute specifies the named cache that will be used. ThedataCacheClientName
attribute specifies whichdataCacheClient
section to use for the cache configuration.
Warning
We recommend that you secure the web.config file used to specify the names of the cache hosts.
To use Microsoft AppFabric 1.1 for Windows Server for output caching
First prepare your Visual Studio 2010 project to use Microsoft AppFabric 1.1 for Windows Server Caching. For more information, see Preparing the Cache Client Development Environment (AppFabric 1.1 Caching).
In addition to the normal caching assemblies, also reference the Microsoft.Web.DistributedCache.dll assembly in the Microsoft AppFabric 1.1 for Windows Server Caching installation path.
Copy the
configSections
element from the example after these steps into your web.config file. Make sure that this element is the first element inside theconfiguration
tags.Copy the
dataCacheClients
element from the example after these steps into your web.config file. It should be added after theconfigSections
element, inside theconfiguration
element.- Configure the
name
andcachePort
attributes of the host elements to match the cache servers in your environment. Add or remove host elements as appropriate.
- Configure the
Copy the
caching
element from the example after these steps into your web.config file. It should be positioned inside thesystem.web
element. Specify thecacheName
anddataCacheClientName
attributes as well as any other required settings.Determine the identity of the web application. This can be done in IIS Manager on the web servers. Look at the identity of the application pool associated with the web application. Grant this user access to the cache cluster using the
Grant-CacheAllowedClientAccount
Windows Powershell command.Tip
If the application pool runs as a built-in machine account, such as "NT Authority\Network Service", then you should grant access to the cache cluster for that machine. You can do this by specifying DOMAINNAME\MACHINENAME$ as the account. Note that the "$" is appended to the machine name to indicate that this is the machine account.
Example
This example shows how to configure an ASP.NET Web application to store output cache data in a distributed cache named default
. The cache client in this example is only configured to communicate with one cache host that is named CacheServer1
.
First, add the
configSections
element to the web.config file as the first element in theconfiguration
element:<!--configSections must be the FIRST element --> <configSections> <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/> </configSections>
Second, add the single
dataCacheClients
element that contains one or moredataCacheClient
elements. Add this to the web.config file, after theconfigSections
element. This is where you configure your cache client to meet the needs of your application. For more information, see Application Configuration Settings (AppFabric 1.1 Caching).<dataCacheClients> <dataCacheClient name="default"> <hosts> <host name="CacheServer1" cachePort="22233" /> </hosts> </dataCacheClient> </dataCacheClients>
After the
configSections
anddataCacheClients
elements have been added, add thecaching
element to the web.config file, inside thesystem.web
element. This is where you specify which cache the Web application will use to store output cache data. Customize thecacheName
anddataCacheClientName
attributes as well as any other required settings.<caching> <outputCache defaultProvider="DistributedCache"> <providers> <add name="DistributedCache" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" /> </providers> </outputCache> </caching>
When it is complete, the Web application's final web.config file will resemble the following example.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="dataCacheClients"
type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core"
allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
<dataCacheClients>
<dataCacheClient name="default" channelOpenTimeout="10000">
<hosts>
<host name="CacheServer1" cachePort="22233" />
</hosts>
</dataCacheClient>
</dataCacheClients>
<system.web>
<caching>
<outputCache defaultProvider="DistributedCache">
<providers>
<add name="DistributedCache"
type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache"
cacheName="default"
dataCacheClientName="default" />
</providers>
</outputCache>
</caching>
</system.web>
</configuration>
See Also
Concepts
Output Cache Provider (AppFabric 1.1 Caching)
2012-09-12