如何在代码中配置Azure缓存服务?

时间:2013-10-12 19:36:50

标签: azure azure-caching

如何在代码中配置Azure缓存服务?注意我正在谈论目前处于预览状态的新缓存服务,该服务使用缓存库的v2 +。我谈论旧的Azure共享缓存服务,它非常相似。

在我的特定情况下,我想在服务配置(.cscfg)文件中配置缓存参数,而不是在web.config中。但也可能还有其他原因。

1 个答案:

答案 0 :(得分:1)

这似乎可以解决问题。

//Utility function
private static SecureString MakeSecureString(string s)
{
    SecureString secureString = new SecureString();
    foreach (char c in s)
    {
        secureString.AppendChar(c);
    }
    secureString.MakeReadOnly();
    return secureString;
}

//Populate these values from whereever you want, perhaps read from config
string host = "foo.cache.windows.net"
string rawAuthToken = "abcdefgblahblahblahfoobar==";

//Create the SecureString that we need for the security config
SecureString authToken = MakeSecureString(rawAuthToken);

DataCacheFactoryConfiguration cacheConfig = new DataCacheFactoryConfiguration();
cacheConfig.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, host);
cacheConfig.SecurityProperties = new DataCacheSecurity(authToken);

var cacheFactory = new DataCacheFactory(cacheConfig);

//Get a cache as normal
DataCache cache = cacheFactory.GetCache("default");