[Solved] In-Role Cache Session Management in AZURE PAAS asp.net c#

In-Role Cache Session Management

In this tutorial I will explain how to configure in-role cache to manage sessions in your Azure PAAS model. One of the major advantage is that, this is free of cost, we don't need any extra instance to manage the session.

Azure Project Role Configuration

First of all we need to enable caching in the corresponding role (use the web role). Right click on the role and take Properties. From there select the Caching pane tick on the Enable Caching and specify the cache size and storage account use for maintaining the cache cluster's runtime state (could use the blob storage account). Refer the below image
How to enable caching in azure roles?
Azure Role Cache Enabling

Windows Azure Caching Installation in Web project

Now we need to install the windows azure cache dll in the web project (in which the web role pointing). Right click on the project and choose Manage Nuget packages. Find the Microsoft.WindowsAzure.Caching and install it.
How to install Windows azure cache from Nuget?
Install Windows Azure Caching from Nuget
After installing Windows Azure Caching your web.config (web project) will be changed, following details are added.

configSections Update

    <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
    <section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere" />

System.Web Session State New Configuration Update

    <!-- Windows Azure Caching session state provider -->
    <sessionState mode="Custom" customProvider="AFCacheSessionStateProvider">
        <add name="AFCacheSessionStateProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheSessionState"/>
    <!-- Windows Azure Caching output caching provider -->
      <outputCache defaultProvider="AFCacheOutputCacheProvider">
          <add name="AFCacheOutputCacheProvider" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheOutputCache" />

Bottom of Web.Config Update

    <dataCacheClient name="default">
      <autoDiscover isEnabled="true" identifier="<Your role name>" />
      <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
    <crashDump dumpLevel="Off" dumpStorageQuotaInMB="100" />

In this bottom of Web.Config update you need to specify your web role name in Azure PAAS project. like as shown below

<autoDiscover isEnabled="true" identifier="SampleWebRole" />

Now you can deploy your azure project, and now the sessions will be stored in Web role cache now. If you have any doubts, just comment here.
[Solved] In-Role Cache Session Management in AZURE PAAS asp.net c# [Solved] In-Role Cache Session Management in AZURE PAAS asp.net c# Reviewed by TechDoubts on 2:18 AM Rating: 5

No comments:

Powered by Blogger.