Using VS2012 I added the caching feature from the WebRole Properties Caching Tab. Among others, it generated the following XML in web.config:
In your question you talk about adding the XML to the web.config, this works for the web application being hosted in your Web Role (that's why the code works when using it in the Application_Start method).
But you need to know that the code in the WebRole.cs runs in a different process (before even startin the web application). That's why it can't read from your web.config and that's the reason why it seemed there was no server configured.
In order to make that code also work from your WebRole.cs you'll need to add the XML in the config file for the process running that code. Your code runs in the WaIISHost.exe process, so you'll need to create a new configuration file WaIISHost.exe.config, add the XML in this file and change the Copy to Output Directory property for that file to "Copy Always".
Read more about this WaIISHost.exe process here: New Full IIS Capabilities: Differences from Hosted Web Core
Yes. Step though the various config and I did reinstall the Azure Tools as well during my troubleshooting.
There is the Web.config which is mentioned above, but in your service definition, ensure it has a line:
<Import moduleName="Caching" />
This was my root issue, but then had changed nearly everything once trying to get it to work. Also, when I looked at the web role, the Caching tab was missing which was fixed by reinstalling Azure tools (I did a clean up of old ones which may or may not have helped). I initiated it via the Azure site examples rather than what is above.
DataCache AgencyCache = new DataCache("AgencyDataValidation");
Remembering to import:
using Microsoft.ApplicationServer.Caching;
I got it to work!
I was creating the DataCacheFactory in WebRole OnStart method, I moved it over to Application_Start in Global.asax and it seems to be working.