AppFabric Caching Used in an ASP.NET Web Application

This sample shows how to write a simple ASP.NET application that uses the AppFabric cache.

The Web application is a simple order processing application. You can create an order, retrieve it, and modify the order. Multiple orders can be stored in the cache at the same time.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples.


Ideally, users should have knowledge of Windows PowerShell scripting.

The sample assumes the following:

  • Windows PowerShell 2.0 is installed.

  • Microsoft AppFabric 1.1 for Windows Server is installed. (This also adds the required DLLs to the GAC.)

Configure the AppFabric Cache

If you have not done so already, configure the AppFabric cache by using the Microsoft AppFabric 1.1 for Windows Server configuration tool (use default settings). The sample assumes that an AppFabric cache is configured with default settings (cacheHostName=AppFabricCachingService and cacheName=default).

Start the Cache Cluster

  1. Open a Windows PowerShell command window in elevated mode and run the following command to add the Distributed Cache administration module:

    Import-Module DistributedCacheAdministration
  2. Run the following command to grant your user account access to the cache cluster as a client. Supply your user and domain name.

    Grant-CacheAllowedClientAccount domain\username
  3. Use Get-CacheAllowedClientAccounts command to verify your user account has been granted access.

  4. Start the cluster by using Start-CacheCluster command.

Build and Run This Sample

The sample automatically links to the AppFabric Cache DLLs (Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll) from the GAC.

  1. Open the CacheSampleWebApp.sln solution in Visual Studio.

  2. Press Ctrl+F5 to run the web project or F5 to debug.

  3. The browser should launch with address under http://localhost:23222/. Use the following addresses while running the project to interact with the web application:

    • http://localhost:23222/CreateOrder.aspx to create a sample order

    • http://localhost:23222/CreateOrder.aspx to create a sample order

    • http://localhost:23222/UpdateOrder.aspx to update the order


If data is not getting added into the cache, it could be that the cache’s eviction policy is getting in the way. To ensure that the cache does not have any eviction policy set, please run the following commands to remove the default cache and recreate it with no eviction policy:

Remove-Cache -CacheName default
New-Cache -CacheName default -Eviction None

Remove This Sample

Restart the cache cluster by using the Restart-CacheCluster command to clean up data from the cache.

See Also