locked
How to: Implement colocated caching RRS feed

  • Question

  • 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?

    Thursday, August 9, 2012 1:47 PM

Answers

  • Hi,

    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:

    http://blogs.msdn.com/b/windowsazure/archive/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core.aspx

    http://msdn.microsoft.com/en-us/library/windowsazure/hh127476.aspx

    Hope this helps.

    BR,

    Arwind


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Friday, August 10, 2012 3:31 AM

All replies

  • 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.

    Thursday, August 9, 2012 2:37 PM
  • Hi,

    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:

    http://blogs.msdn.com/b/windowsazure/archive/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core.aspx

    http://msdn.microsoft.com/en-us/library/windowsazure/hh127476.aspx

    Hope this helps.

    BR,

    Arwind


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Friday, August 10, 2012 3:31 AM