I recently integrated Windows Azure’s AppFabric Cache into a solution I’ve been working on and went down the programmatic route as opposed to modifying my solution’s application configuration files. I initially modified the web.config and app.config files for my web and worker roles respectively, just to see how easy it was to implement caching via that method, and I can say with confidence that Microsoft have made it a complete breeze. However, my requirements meant that I had to programmatically create the DataCacheFactory and connect to the SSL endpoint for an added layer of security.
This wasn’t exactly rocket science, and after reading the following articles I was ready to start coding:
- How to Use the Caching Service (Windows Azure)
- How to Configure a Cache Client Programmatically for Windows Azure Caching (MSDN)
- Caching Service Usage Quotes (MSDN)
- Understanding and Managing Connections in Windows Azure (MSDN)
The one thing I couldn’t find any information on was how to connect to the SSL endpoint programmatically. So I did a bit more reading into the DataCacheSecurity object and realised that all I needed was to use one of it’s overloaded constructors which provided the option to enable SSL.
So instead of instantiating the DataCacheSecurity object like so:
DataCacheSecurity dataCacheSecurity = new DataCacheSecurity(secureAuthenticationToken);
I added the following parameter:
DataCacheSecurity dataCacheSecurity = new DataCacheSecurity(secureAuthenticationToken, true);
(See the How to Configure a Cache Client Programmatically for Windows Azure Caching article for the remainder of the code)
In addition, I had to ensure I was using the correct cache port of 22243, but if you’ve read this far I’m sure you’ll have guessed that already. Everything ran perfectly from here and after a few hours of testing my solution was up in and running in the cloud.
Programmatic Integration or Configuration Integration?
Which approach you take depends on the solution at hand. I had a solution that was similar to the one illustrated below (slightly more complex but I don’t want to let that sidetrack the point I’m trying to make). This particular solution has to be able to run in several environments; development, staging and production (as per the norm) as well as a sandbox environment for staff who work abroad that need a place to practice using new features before their released. Using the default web.config and app.config didn’t provide the flexibility required – I needed to be able to configure my particular environment’s service configuration file to use the correct caching namespace. So for me programmatically configuring my caching client was the only option.
(These icons can be sourced from David Pullman’ s Blog)
If you are looking to implement AppFabric Caching then I’d recommend reading the following points (please excuse me if I’m stating the obvious):
- Ensure you have the Windows Azure SDK v1.6 (November 2011) or greater
- Be careful where you’re sourcing your information, some of it can be dated and cause confusion, or just not work anymore
- The updates Microsoft made to their Windows Azure website back in December 2011 are fantastic so it’s a good place to start if you’re interested in learning more about, or implementing, a given Azure service