【问题标题】:How do I configure Azure Cache Service in code?如何在代码中配置 Azure 缓存服务?
【发布时间】:2013-12-14 06:55:07
【问题描述】:

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

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

【问题讨论】:

  • 我很好奇投反对票的目的是什么?由于有人刚刚通过并否决了我自己回答的另外两个问题,我只想指出这实际上是 StackOverflow 的一个完全支持的功能。 “提问”表单上甚至还有一个“回答您自己的问题 - 分享您的知识问答方式”复选框。

标签: azure azure-caching


【解决方案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");

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多