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

本主题介绍如何准备您的 Visual Studio 项目以便可以开始开发启用缓存的应用程序。 以下过程假设您已经安装了 Windows Server AppFabric 并在一台或更多缓存服务器,或在您的开发工作站上配置了 AppFabric 缓存功能。 有关详细信息,请参阅 Windows Server AppFabric 安装指南 (https://go.microsoft.com/fwlink/?LinkId=169172)。

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

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

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

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

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

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

准备开发计算机

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

准备 Visual Studio 项目

开发启用缓存的应用程序所需的程序集将安装在全局程序集缓存 (GAC) 中。 若要开发使用这些程序集的 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. 导航到 .\Windows\System32\AppFabric 目录。

    备注

    在 64 位操作系统中,无法直接看到 AppFabric 目录。 若要解决此问题,请使用 SysNative 名称替换 System32 目录名称。 这将会导航到此步骤中的 C:\Windows\SysNative\AppFabric 目录。

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

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

配置缓存客户端的步骤

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

  2. 以编程方式或使用应用程序配置文件配置您的缓存客户端。 有关如何执行此操作的示例,请参阅Windows Server AppFabric 缓存客户端入门Windows Server AppFabric 缓存客户端 (XML) 入门

示例

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

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

另请参阅

概念

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

其他资源

用 XML 配置缓存客户端

  2011-12-05