Implement azure colocated caching

Viewed 2801

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

3 Answers
Related