Using VS2012 I added the caching feature from the WebRole Properties Caching Tab. Among others, it generated the following XML in web.config:
<dataCacheClients>
<tracing sinkType="DiagnosticSink" traceLevel="Error" />
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="[cluster role name]"/>
<!-- <localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" /> -->
</dataCacheClient>
</dataCacheClients>
Okay, great. I replaced the [cluster role name] with the name of the webrole, say "helloworld.web." Now, when I create the DataCacheFactory or DataCache object:
_dataCacheFactory = new DataCacheFactory();
_defaultCache = _dataCacheFactory.GetDefaultCache();
//Or, just this line
_defaultCache = new DataCache(@"default");
I get the following error:
Microsoft.ApplicationServer.Caching.DataCacheException was unhandled
HelpLink=http://go.microsoft.com/fwlink/?LinkId=164049
HResult=-2146233088
Message=ErrorCode<ERRCA0021>:SubStatus<ES0001>:Server collection cannot be empty.
Source=Microsoft.ApplicationServer.Caching.Client
ErrorCode=21
SubStatus=-1
Some notes:
IDE: VS2012,
Framework: 4.0
AzureSDK: June2012
Local dev machine
What am I missing?
Edit
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.
Sandrino explains why this is the case: https://stackoverflow.com/a/11886136/1374935
To create a cache, sign in to the Azure portal and select Create a resource. On the New page, select Databases and then select Azure Cache for Redis. On the New Redis Cache page, configure the settings for your new cache. Drop down and select your subscription.
The Azure App Service Local Cache feature provides a web role view of your content. This content is a write-but-discard cache of your storage content that is created asynchronously on-site startup. When the cache is ready, the site is switched to run against the cached content.
Azure Cache for Redis is a fully managed, in-memory cache that enables high-performance and scalable architectures. Use it to create cloud or hybrid deployments that handle millions of requests per second at sub-millisecond latency—all with the configuration, security, and availability benefits of a managed service.
A cache-aside cache is the most common caching strategy available. The fundamental data retrieval logic can be summarized as follows: When your application needs to read data from the database, it checks the cache first to determine whether the data is available.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With