How to Use the cache service to share sessions in windiwsazure
After a multi-instance webrole (that is, the instancecount of webrole is greater than 1) is released on windowsazure, because the azure Load Balancing Program cyclically calls multiple instances, by default, sessions stored in the memory will be messy and cause application paralysis.
To solve this problem, you can store sessions in SQL azure to share sessions among multiple instances. However, the author provides another method to store sessions: Use the appfabric cache service to store sessions.
The procedure is as follows:
1. Activate the appfabric cache service.
Access the http://portal.appfabriclabs.com this CTP portal, select the cache option on the portal, and then click New namespace (New namespace) to create a new cache. In the pop-up dialog box, set the unique service namespace and cache size, and click OK to activate the service. Then, a service URL and authentication token are obtained ). The service URL is your application and caching
The TCP endpoint to be connected during service interaction. An authentication token is an encrypted token that is passed to access control to verify your service.
2. Add all the Assembly provided by the appfabric SDK to the webrole of the azure project.
These Assemblies include:
Microsoft. applicationservice. caching. Client
Microsoft. applicationservice. caching. Core
Microsoft. Web. distributedcache
Microsoft. windowsfabric. Common
Microsoft. windowsfabric. Data. Common
3. Modify the Web. config file to enable appfabric cache for the application.
Make the <configuration> element followed by the <configsections> and <datacacheclient> elements (if it is followed by another element, you will receive an error ).
<?xml version="1.0"?><configuration> <configSections> <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <dataCacheClient>
4. Modify the Web. config file so that the application session is stored in the cache service.
<?xml version="1.0"?><configuration> <system.web> <sessionState mode="Custom" timeout="20" customProvider="AppFabricCacheSessionStoreProvider"> <providers> <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default"/> </providers> </sessionState> </system.web> </configuration>
By now, all the settings required to store the session in the cache service have been completed. Now, the session value is stored in the cache service, it can be shared among multiple instances. You can create an example and test it on your own.
In the cache service, since the session value can be stored, of course, all the values that the application wants to store can be stored, such as the user's login name and intermediate computing results. The specific operation example is as follows:
static void Main(string[] args) { DataCacheFactory dataCacheFactory = new DataCacheFactory(); DataCache dataCache = dataCacheFactory.GetDefaultCache(); Console.Write("Enter a value: "); string value = Console.ReadLine(); dataCache.Put("key", value); string response = (string)dataCache.Get("key"); Console.WriteLine("Your value: " + response); }
Finally, we will summarize the functions that the caching Service provides for developers:
1. the preset ASP. net provider for session Status and page output cache allows you to accelerate Web applications without modifying application code.
2. cache all hosted objects. There is no limit on the object size and there is no serialization overhead required for local cache.