How to: Configure a Cache Client using the Application Configuration File (Windows Azure Shared Caching)
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 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 (Windows Azure Shared Caching).
Note |
|---|
| 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 Caching API Support in Windows Azure Caching. |
To configure the cache client with an application configuration file
-
First prepare your Visual Studio 2012 project to use Windows Azure Caching. For more information, see How to: Prepare Visual Studio to Use Caching for Windows Azure.
-
On the Management Portal, view the settings for the target cache. For more information, see Managing Caches (Windows Azure Shared Caching).
-
In the Properties window, note the value of cache Service URL. This is the host name for your Windows 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 Windows 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. DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration("default"); DataCacheFactory cacheFactory = new DataCacheFactory(config); DataCache defaultCache = cacheFactory.GetDefaultCache(); // Put and retrieve a test object from the default cache. defaultCache.Put("testkey", "testobject"); string strObject = (string)defaultCache.Get("testkey");
See Also
Build Date: