DCSIMG
How to use the new Distributed Cache (SDK 1.7) for Asp.Net Session State - Manu Cohen-Yashar's Blog

Manu Cohen-Yashar's Blog

How to use the new Distributed Cache (SDK 1.7) for Asp.Net Session State

Until today AppFabric Distributed cache was used to host Azure web roles Asp.Net Session state. Unfortunately there were many problems with AppFabric Azure Distributed Cache:

  1. It was VERY expensive.
  2. It was not available in all Data Centers.
  3. It was slow.

Fortunately in the new SDK (1.7) we have the opportunity to use our resources (i.e. roles) to host the distributed cache cluster and get a free and performant cache.

So lets see how to use it to host asp.net session:

1. For each role that will donate some resource for the distributed cache cluster we should set the cache configuration. Open the new caching tab in the role configuration and enable the cache.

  • Enable the cache
  • Set the deployment policy (use existing roles or create dedicated ones)
  • Provide a valid connection string to storage
  • set the caching policy (sliding / fix expiration), TTL etc.

image

2. Now it is time to add code to the actual web role. First install the NuGet package by searching
for: "windowsazure.caching" and installing "Windows Azure Caching Preview".

image

The installation will add some configuration to web.config.
Set the name of the role (that hosts the cache) in the identifier property:

image 

Now we have to set the session state provider by overriding the session provider configuration to:

 <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
   <providers>
     <add name="AppFabricCacheSessionStoreProvider"  
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" 
          cacheName="default" useBlobMode="true" dataCacheClientName="default" />
      </providers>
 </sessionState>

The final step is of course to use the session in our application:

protected void btnClick_Click(object sender, EventArgs e)
{
   if (Session["count"] == null)
      Session.Add("count", 0);            
   // increment the counter
  
Session["count"] = (int)Session["count"] + 1; 
   lblCount.Text = Session["count"].ToString();
}

So Simple…

Enjoy

Manu

Comments

William said:

Frankly speaking, Appfabric still has to come up a long way to compete with some of the existing and well mature distributed cache providers. Just take the example of Ncache. It offers a wide range of features which AppFabric is not offering yet. Watch the video given in the link below to learn about the comparison of features of NCache Vs AppFabric.

www.alachisoft.com/.../ncache-vs-appfabric.html

# July 26, 2012 12:03 PM

ViekSeeds said:

Nike Unencumbered TR Hale 2 Bulwark topmost of textile materials in wire with seasonal requirements, enhances the warmth and durability, and contemplative bodily, in [url=www.nikeskoroutlet.se/nike-air-max-90-c-1]air max hyperfuse[/url]

barren visibility when light and DWR (sturdy inundate stubborn) coating to carry out breathability while, cause to feet wilt in wet weather. Shoe upper knock on the by nature of the salaam effervescence enhances foot support and stability.

Complexion ratio of the Nike Permitted shoe Groove work provides springy like tell feet handle and sturdiness of multi-direction action while [url=www.airmax90rea.se/nike-air-max-2013-c-16]nike air max 2013[/url]

retaining cope and shockproof star engrained in training shoes. Lightweight Phylite midsole can unseat and masses of durability, which increments the usefulness exponentially outsole, which significantly reduces the weight of the shoe.

Bounder and foot rubber grooves made of environmentally buddy-buddy materials, help to enhance its [url=www.airmax1rea.se/nike-air-max-lunar-c-31]air max luner[/url]

multi-directional understanding on all kinds of road surfaces.

# April 21, 2013 9:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: