How to: Configure a Cache Client using the Application Configuration File (Azure Shared Caching)
Updated: June 19, 2014
|Please note the Azure Shared Caching service will be retired on September 1, 2014 and with it the Azure Silverlight-based portal. Once the Shared Caching service is retired, all remaining Shared Cache deployments will be deleted. Microsoft strongly encourages you to migrate at the earliest opportunity all existing Shared caches to either the Managed Cache Service (currently in GA) or to the new Azure Redis Cache (currently in Preview). For migration guidance, including guidance for migrating without making code changes, see Migrate from Shared Caching. For more information about the current Azure Cache offerings, see Azure Cache.|
This topic explains how to configure a cache client to use a provisioned Microsoft 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 covers the use of the application configuration file. To see how to perform the same configuration programmatically, see How to: Configure a Cache Client Programmatically (Azure Shared Caching).
|Much of the cache client programming model for the cloud-based Microsoft Azure is shared with the on-premises Microsoft AppFabric 1.1 for Windows Server. For more information, see Cache API Support in Azure Cache.|
To configure the cache client with an application configuration file
First prepare your Visual Studio 2012 project to use Microsoft Azure Cache. For more information, see How to: Prepare Visual Studio to Use In-Role Cache (Azure Cache).
On the Management Portal, view the settings for the target cache. For more information, see Managing Caches (Azure Shared Caching).
In the Properties window, note the value of cache Service URL. This is the host name for your Microsoft Azure Cache. It can also be referred to as the cache endpoint URI.
On the Properties window, also note the value of the cache Authentication Token. This is an ACS key that secures access to your cache.
Click the View Client Configuration button on the toolbar.
In the pop-up Client Configuration window, note the XML snippets. Add these snippets to your projects application configuration file (app.config) or web configuration file (web.config) in the configuration section.
Warning The configSections element must be the first element in the configuration section. If there is an existing configSections element, just add the copied section within it. Note Note that the sessionState and outputcache elements are only used for ASP.NET applications. You should not copy these into the application configuration file of a non-ASP.NET cache client.
In your application, create a new DataCacheFactoryConfiguration object.
Use an empty constructor to load the "default" configuration settings. This also applies if you only have one unnamed dataCacheClient section.
Use a string in the constructor to load a named configuration section. For example, the portal automatically provides a dataCacheClient section named "SslEndpoint".
- Use an empty constructor to load the "default" configuration settings. This also applies if you only have one unnamed dataCacheClient section.
Create a DataCacheFactory object and use the constructor to pass the previously created DataCacheFactoryConfiguration object.
Use the DataCacheFactory object to call GetDefaultCache. This returns a DataCache object that can be used to programmatically access the cache.
The following shows an example of an app.config file configured to use Microsoft Azure. The actual values in your configuration file will be different.
<?xml version="1.0"?> <configuration> <configSections> <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <!-- Cache exposes two endpoints: one simple and other SSL endpoint. Choose the appropriate endpoint depending on your security needs. --> <dataCacheClients> <dataCacheClient name="default"> <hosts> <host name="yourcachename.cache.windows.net" cachePort="22233" /> </hosts> <securityProperties mode="Message"> <messageSecurity authorizationInfo="iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmludDMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lFQ2FFPSZodJRzOi8xS8JxdGJEb281bzxudGF0az9uxGxzdGluZi5jiz8oZS5pb8QzL8dpbmRxd3MQaz50Lm5ldA=="> </messageSecurity> </securityProperties> </dataCacheClient> <dataCacheClient name="SslEndpoint"> <hosts> <host name="yourcachename.cache.windows.net" cachePort="22243" /> </hosts> <securityProperties mode="Message" sslEnabled="true"> <messageSecurity authorizationInfo="iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmludDMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lFQ2FFPSZodJRzOi8xS8JxdGJEb281bzxudGF0az9uxGxzdGluZi5jiz8oZS5pb8QzL8dpbmRxd3MQaz50Lm5ldA=="> </messageSecurity> </securityProperties> </dataCacheClient> </dataCacheClients> </configuration>
The code to obtain a DataCache using the application configuration file is shown below.
' Cache client configured by settings in application configuration file. Dim config As New DataCacheFactoryConfiguration("default") Dim cacheFactory As New DataCacheFactory(config) Dim defaultCache As DataCache = cacheFactory.GetDefaultCache() ' Put and retrieve a test object from the default cache. defaultCache.Put("testkey", "testobject") Dim strObject As String = defaultCache.Get("testkey")