准备缓存客户端开发环境(AppFabric 1.1 缓存)

本主题介绍如何准备您的 Visual Studio 项目以便可以开始开发启用缓存的应用程序。以下过程假定您已在一台或形成缓存群集的多台缓存服务器上安装 Microsoft AppFabric 1.1 for Windows Server 并配置了 AppFabric 缓存 功能。有关详细信息,请参阅安装 AppFabric 1.1

除了安装 AppFabric 缓存 功能之外,在缓存客户端可以访问缓存之前,还必须执行以下步骤:

  • 在 Windows PowerShell 中,使用 Use-CacheCluster 设置目标缓存群集的上下文。

  • 使用 New-Cache 命令创建所有必要的命名缓存。

  • 使用 Grant-CacheAllowedClientAccount 命令为缓存客户端的 Windows 帐户授予访问权限。

  • 使用 Start-CacheCluster 命令启动缓存群集。

有关使用 Windows PowerShell 和此处所列命令的详细信息,请参阅使用 Windows PowerShell 管理 AppFabric 1.1 缓存功能

准备开发计算机

若要开发使用 AppFabric 缓存 功能的应用程序,唯一的安装要求是安装了 AppFabric 的缓存客户端功能。可以在安装了其他 AppFabric 缓存 功能的缓存主机上进行开发,但在开发工作站上只要求安装缓存客户端功能。

准备 Visual Studio 项目

开发启用缓存的应用程序所需的程序集位于 Microsoft AppFabric 1.1 for Windows Server 的安装目录中。默认位置为 .\Program Files\Windows Server AppFabric。若要开发使用这些程序集的 Visual Studio .NET 应用程序,您必须从您的项目中引用它们。

以 .NET Framework 的正确版本为目标

  1. 打开您的 Visual Studio .NET 项目。

  2. 在“解决方案资源管理器”中,右键单击该项目名称,然后选择“属性”。

  3. 选择“项目属性”对话框的“应用程序”选项卡。

  4. 验证目标框架版本是否为 .NET Framework 2.0 或更高版本。

    Important要点
    不使用目标框架版本的客户端配置文件。在 Visual Studio 2008 中,取消选中“仅客户端框架子集”复选框。在 Visual Studio 2010 中,选择未指定“客户端配置文件”的 .NET Framework 版本。

添加对 AppFabric 缓存 程序集的引用的步骤

  1. 打开您的 Visual Studio .NET 项目。

  2. 在“解决方案资源管理器”中,右键单击该项目名称,然后选择“添加引用”。

  3. 选择“添加引用”对话框的“浏览”选项卡。

  4. 导航到 AppFabric 安装目录(默认位置为 .\Program Files\Windows Server AppFabric)。

  5. 添加对以下两个程序集的引用:Microsoft.ApplicationServer.Caching.Client.dll 和 Microsoft.ApplicationServer.Caching.Core.dll。

  6. 或者,可以在代码文件的顶部添加 using 语句(在 Visual Basic 中为 Imports)来引用 Microsoft.ApplicationServer.Caching 命名空间。

配置缓存客户端的步骤

  1. 为客户端应用程序确定适当的客户端设置。有关缓存客户端类型的详细信息,请参阅缓存客户端和本地缓存(AppFabric 1.1 缓存)

  2. 以编程方式或使用应用程序配置文件配置您的缓存客户端。有关如何执行此操作的示例,请参阅缓存客户端入门缓存客户端入门 (XML)应用程序配置设置(AppFabric 1.1 缓存) 中提供了缓存客户端配置选项的完整列表。

示例

以下是使用应用程序配置文件配置的一个缓存客户端示例。本示例禁用了本地缓存并列出了两个缓存主机:CacheServer1CacheServer2。有关详细信息,请参阅缓存客户端入门 (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>

下例演示了如何以编程方式来配置客户端。此例禁用了本地缓存,仅列出了一个缓存服务器 (CacheServer2),并为名为 NamedCache1 的缓存创建了一个缓存客户端。有关详细信息,请参阅缓存客户端入门

' 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");

另请参阅

概念

应用程序配置设置(AppFabric 1.1 缓存)
配置缓存客户端
基于 XML 的客户端配置
编程客户端配置
使用基本缓存方法
使用配置方法
AppFabric 缓存概念(AppFabric 1.1 缓存)

  2012-03-05