09 Ağustos 2012 Perşembe 13:47
Using VS2012 I added the caching feature from the WebRole Properties Caching Tab. Among others, it generated the following XML in web.config:
<tracing sinkType="DiagnosticSink" traceLevel="Error" />
<autoDiscover isEnabled="true" identifier="[cluster role name]"/>
<!-- <localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" /> -->
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
Local dev machine
What am I missing?
09 Ağustos 2012 Perşembe 14:37
Everything is good.
I was creating the DataCacheFactory in WebRole OnStart method, I moved it over to Application_Start and it seems to be working.
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.
10 Ağustos 2012 Cuma 03:31Moderatör
As far as i know, Application_Start() method in global.asax is happened after RoleEntryPoint.OnStart() mehtod. You may put your initialization code there. Here is the dependency:
your RoleEntryPoint code cannot-because it's not running as a part of your web site. As mentioned earlier, it runs under a process called WaIISHost.exe, so it expects its configuration to be in a file called WaIISHost.exe.config. Therefore, if you create a file with this name in the your web project and set the "Copy to Output Directory" property to "Copy Always" you'll find that the RoleEntryPoint can read this happily. This is one of the only cases I can think of where you'll have two .NET configuration files in the same project
If you're using HWC with previous versions of the SDK (or if you deleted the <Sites> configuration setting with SDK 1.3), you can happily put this code in WebRole.OnStart. However as soon as you move to Full IIS, the call to CloudStorageAccount.FromConfigurationSetting will fail with an InvalidOperationException:
SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used
"But I did call it!" you'll scream at your computer (well at least that's what I did). And indeed you did-however you called it in an AppDomain in the WaIISHost.exe process, which has no effect of your web site hosted in an entirely different AppDomain under IIS. The solution is to make sure you call CloudStorageAccount.SetConfigurationSettingPublisher and CloudStorageAccount.FromConfigurationSetting within the same AppDomain, most likely from your web site. While there used to be some issues with accessing Windows Azure SDK classes in yourApplication_Start event, these no longer apply and this is a great place to initialise your configuration setting publisher
Simple to say, the OnStart method will be invoked by WaIISHost.exe, but Application_Start method will be invoked by w3wp.exe. Full IIS Capabilities is default mode in later Azuare 1.3 SDK, dont please some initialize code logic in RoleEntryPoint event (Azure Caching service, Storage connection, etc).
Here you can find more information about HWC & Full IIS capabilities and Role startup lifecycle:
Hope this helps.