0

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.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Brian Reischl
  • 7,216
  • 2
  • 35
  • 46
  • 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 Jun 04 '14 at 19:30

1 Answers1

1

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");
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Brian Reischl
  • 7,216
  • 2
  • 35
  • 46