Preparing the Cache Client Development Environment (AppFabric 1.1 Caching)

This topic describes how to prepare your Visual Studio project so that you can start developing cache-enabled applications. The following procedures assume that you have already installed Microsoft AppFabric 1.1 for Windows Server and configured the AppFabric Caching features on one or more cache servers, which results in a cache cluster. For more information, see Installing AppFabric 1.1.

In addition to installing AppFabric Caching features, you must perform the following steps before the cache client can access the cache:

  • In Windows PowerShell, use the Use-CacheCluster to set the context to the target cache cluster.

  • Create any necessary named caches with the New-Cache command.

  • Grant access to the cache client's Windows account with the Grant-CacheAllowedClientAccount command.

  • Start the cache cluster with the Start-CacheCluster command.

For more information about using Windows PowerShell and the commands listed here, see Using Windows PowerShell to Manage AppFabric 1.1 Caching Features.

Preparing the Development Computer

To develop an application that uses the AppFabric Caching features, the only setup requirement is to have the Cache Client feature of AppFabric installed. It is possible to develop on a cache host that has other AppFabric Caching features installed, but the Cache Client feature is the only requirement on the development workstation.

Preparing the Visual Studio Project

The assemblies required for developing cache-enabled applications are located in the installation directory of Microsoft AppFabric 1.1 for Windows Server. The default location is .\Program Files\Windows Server AppFabric. To develop a Visual Studio .NET application that uses these assemblies, you must reference them from your project.

To target the correct version of the .NET Framework

  1. Open your Visual Studio .NET project.

  2. In Solution Explorer, right-click the project name and then select Properties.

  3. Select the Application tab of the Project Properties dialog.

  4. Verify that the target framework version is .NET Framework 2.0 or higher.

    Important

    Do not use the client profile for the target framework version. In Visual Studio 2008, uncheck the Client-only Framework subset checkbox. In Visual Studio 2010, select .NET Framework versions that do not specify "Client Profile".

To add references to the AppFabric Caching assemblies

  1. Open your Visual Studio .NET project.

  2. In Solution Explorer, right-click the project name and then select Add Reference.

  3. Select the Browse tab of the Add Reference dialog.

  4. Navigate to the AppFabric installation directory (defaults to .\Program Files\Windows Server AppFabric ).

  5. Add a reference to the following two assemblies: Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll.

  6. Optionally, add the using statement (Imports in Visual Basic) at the top of the code files to reference the Microsoft.ApplicationServer.Caching namespace.

To configure the cache client

  1. Determine the appropriate client settings for the client application. For more about the cache client types, see Cache Clients and Local Cache (AppFabric 1.1 Caching).

  2. Configure your cache client either programmatically or by using an application configuration file. For examples about how to do this, see Get Started with a Cache Client and Get Started with a Cache Client (XML). The full list of cache client configuration options is provided in Application Configuration Settings (AppFabric 1.1 Caching).

Example

The following is an example of a cache client configured by using an application configuration file. This example has local cache disabled and two cache hosts listed: CacheServer1 and CacheServer2. For more information, see Get Started with a Cache Client (XML).

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <!--configSections must be the FIRST element -->
   <configSections>
      <!-- required to read the <dataCacheClient> element -->
      <section name="dataCacheClient"
         type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
            Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, 
            Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         allowLocation="true"
         allowDefinition="Everywhere"/>
   </configSections>

   <dataCacheClient>
      <!-- (optional) specify local cache
      <localCache
         isEnabled="true"
         sync="TimeoutBased"
         objectCount="100000"
         ttlValue="300" /> -->

      <!--(optional) specify cache notifications poll interval
      <clientNotification pollInterval="300" /> -->

      <hosts>
         <host
            name="CacheServer1"
            cachePort="22233"/>
         <host
            name="CacheServer2"
            cachePort="22233"/>
      </hosts>
   </dataCacheClient>
</configuration>

The following is an example of how to configure a client programmatically. This example has the local cache disabled, lists only one cache server (CacheServer2), and creates a cache client for a cache named NamedCache1. For more information, see Get Started with a Cache Client.

' Declare array for cache host(s).
Dim servers(0) As DataCacheServerEndpoint
servers(0) = New DataCacheServerEndpoint("CacheServer2", 22233)

' Setup the DataCacheFactory configuration.
Dim factoryConfig As DataCacheFactoryConfiguration
factoryConfig = New DataCacheFactoryConfiguration
factoryConfig.Servers = servers

' Create a configured DataCacheFactory object.
Dim mycacheFactory As DataCacheFactory
mycacheFactory = New DataCacheFactory(factoryConfig)

' Get a cache client for the cache "NamedCache1".
Dim myDefaultCache As DataCache
myDefaultCache = mycacheFactory.GetCache("NamedCache1")
// Declare array for cache host(s).
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint("CacheServer2", 22233);

// Setup the DataCacheFactory configuration.
DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
factoryConfig.Servers = servers;

// Create a configured DataCacheFactory object.
DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig);

// Get a cache client for the cache "NamedCache1".
DataCache myDefaultCache = mycacheFactory.GetCache("NamedCache1");

See Also

Concepts

Application Configuration Settings (AppFabric 1.1 Caching)
Configuring a Cache Client
XML-Based Client Configuration
Programmatic Client Configuration
Using Basic Cache Methods
Using Configuration Methods
AppFabric Caching Concepts (AppFabric 1.1 Caching)

  2012-09-12