準備快取用戶端開發環境 (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 或更新版本。

    重要

    請勿使用目標架構版本的用戶端設定檔。 在 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 and 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