Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

How to: Configure a Cache Client Programmatically (Windows Azure Shared Caching)

Updated: February 24, 2014

This topic explains how to configure a cache client to use a provisioned Windows Azure Cache. A cache client is any application that accesses the cache programmatically. The configuration can be done programmatically or through an application configuration file. This topic demonstrates how to programmatically configure the cache client. To see how to use the alternative technique of the configuration file, see How to: Configure a Cache Client using the Application Configuration File (Windows Azure Shared Caching).

Much of the cache client programming model for the cloud-based Windows Azure is shared with the on-premises Microsoft AppFabric 1.1 for Windows Server. For more information, see Cache API Support in Windows Azure Cache.

To programmatically configure the cache client

  1. First prepare your Visual Studio 2012 project to use Windows Azure Cache. For more information, see How to: Prepare Visual Studio to Use In-Role Cache (Windows Azure Cache).

  2. On the Management Portal, view the settings for the target cache. For more information, see Managing Caches (Windows Azure Shared Caching).

  3. Create an instance of the DataCacheFactoryConfiguration class.

  4. Create an array with one element of the type DataCacheServerEndpoint.

  5. In the Properties window, note the value of cache Service URL. This is the host name for your Windows Azure cache. This is the host name for your Windows Azure Labs cache. Construct a new DataCacheServerEndpoint object with this host name and the default port 22233. Assign this to your one-element array.

  6. Assign the DataCacheServerEndpoint array to the Servers property of the DataCacheFactoryConfiguration object.

  7. On the Properties window, also note the value of the cache Authentication Token. This is an ACS key that secures access to your cache.

  8. The ACS key must be passed to the DataCacheSecurity constructor as a System.Security.SecureString. To create the SecureString, use code similar to the following example:

    string strACSKey = "iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmlud" +
        "DMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lF" +
    var secureACSKey = new SecureString();
    foreach (char a in strACSKey)
    This step requires a reference to the System.Security namespace.

  9. Create a new DataCacheSecurity object, and pass the Authentication Token as a SecureString to the constructor.

  10. Assign the DataCacheSecurity object to the SecurityProperties property of the DataCacheFactoryConfiguration object.

  11. Create a new DataCacheFactory object, and pass the DataCacheFactoryConfiguration object to the constructor.

  12. Use the DataCacheFactory object to call GetDefaultCache. This returns a DataCache object that can be used to programmatically access the cache.

The following code example shows a programmatic cache client configuration.

// Declare array for cache host.
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint("yourcachename.cache.windows.net", 22233);

// Setup DataCacheSecurity configuration.
string strACSKey = "iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmlud" +
    "DMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lF" +
var secureACSKey = new SecureString();
foreach (char a in strACSKey)
DataCacheSecurity factorySecurity = new DataCacheSecurity(secureACSKey);

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

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

// Get a cache client for the default cache.
DataCache defaultCache = cacheFactory.GetDefaultCache();

// Add and retrieve a test object from the default cache.
defaultCache.Add("testkey", "testobject");
string strObject = (string)defaultCache.Get("testkey");

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.