如何在代码中配置Azure缓存服务?注意我正在谈论目前处于预览状态的新缓存服务,该服务使用缓存库的v2 +。我不谈论旧的Azure共享缓存服务,它非常相似。
在我的特定情况下,我想在服务配置(.cscfg)文件中配置缓存参数,而不是在web.config中。但也可能还有其他原因。
答案 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");