7

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

Community
  • 1
  • 1
States
  • 548
  • 5
  • 15

3 Answers3

6

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

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
1

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.

States
  • 548
  • 5
  • 15
  • I would like to know why though, and where would be the best place to put the code that initializes certain operations such as creating azure tables, creating cache connections etc for a ASP.NET MVC app. – States Aug 09 '12 at 14:39
  • 1
    I have exactly the same issue but initiating the cache from an MVC Controller. I haven't done anything in Appplication_start. Any ideas, is it a similar issue and I am missing something in the startup? – Steve Newton Oct 22 '13 at 21:44
  • @SteveNewton: I have the same problem. Did you ever figure it out? – Dave New Jun 10 '14 at 05:43
1

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;
Steve Newton
  • 1,046
  • 1
  • 11
  • 28