0
votes

How do I configure the Azure Caching Service in code? Note I'm talking specifically about the new Caching Service, currently in preview, that uses v2+ of the caching libraries. I am not talking about the older Azure Shared Caching service, which is very similar.

In my particular case I want to configure caching parameters in the service configuration (.cscfg) file, rather than in web.config. But there are probably other reasons as well.

1
I'm curious what the downvote was for? Since someone just went through and downvoted two other questions that I answered myself, I'll just point out that that is actually a fully supported feature of StackOverflow. There's even a checkbox for "answer your own question - share your knowledge Q&A style" on the "Ask a question" form. - Brian Reischl

1 Answers

1
votes

This seems to do the trick.

//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");