问题
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
回答1:
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
回答2:
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.
回答3:
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;
来源:https://stackoverflow.com/questions/11885308/implement-azure-colocated-caching