Partilhar via


ASP.NET Session state broken after migrating from Windows Azure shared Cache to Role Based Cache

Windows Azure Shared Cache feature is going to depreciate soon and right now Microsoft is not allowing the end users to create new shared cache namespace from the management portal.

One of the Microsoft partner, who was leveraging Shared Cache to managing the ASP.NET Session state for their Deployed CloudServices got this information from some resources, and approached me to understand the alternatives. I have suggested them to go for a Role based cache feature as mentioned in this article, and provided below mentioned msdn document for smooth migration from shared cache to Role based Cache :

  • Migrate from Windows Azure Shared Caching to In-Role Cache                                                                                                          https://msdn.microsoft.com/en-us/library/windowsazure/hh914160.aspx

This particular partner was on Windows Azure SDK 1.7, which doesn’t support the Role based Cache (RTM) feature, so they have migrated their CloudService application to latest version of Windows Azure SDK, and then migrated to Role based cache by following the above mentioned msdn article. The migration part went smoothly, however while accessing the session state from application, they were start getting below mentioned exception.

 “Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. 
Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the 
<configuration>\<system.web>\<httpModules> section in the application configuration.”

I am providing the complete investigation details below, so that, this could be helpful to troubleshoot, if you ever came across similar issue. Incase if you’re not interested in the complete investigation details, please scroll down to bottom of this page, and you can implement the suggestions provided under RESOLUTION section.

INVESTIGATION DETAILS 

> In general, the above mentioned exception related to ASP.NET sessions will be thrown due to one of the below reasons

  1. Session is not configured properly or explicitly disabled the SessionState by changing enableSessionState property to false.                                                                                               
  2. The modules/binaries for session state management were missing

> Rule out the possible configuration, I have reviewed the web.config and page directive, however, I couldn’t find any thing suspicions, which caused this behavior.

> Rule out the issue with the cache module, I have manually checked the project references for cache references added by Cache SDK 2.2 NuGet package,and I can see all the required binaries for managing role based cache were already added in the project reference.

> To get the more granular details on the issue, I have created a new .aspx page, and added below mentioned code snippet to add data to the Cache object.

 <snip>
DataCacheFactory factory = new DataCacheFactory();
DataCache cache = factory.GetCache("default");
cache.Put("itemData", "datacache");
<snip>

> After running the page with above code snippet throws more meaningful error message as mentioned below :

 System. MissingFieldException was unhandled by user code
  HResult=-2146233071
  Message=Field not found: 'Microsoft.Fabric.Common.IOCompletionPortWorkQueue.WORKER_THREAD_IDLE_WAIT_TIME'.
  Source=Microsoft.ApplicationServer.Caching.Client
  StackTrace:
     at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetNewClientDrm(String cacheName, SimpleSendReceiveModule tempModule, 
     at Microsoft.ApplicationServer.Caching.DataCacheFactory.CreateRoutingClient(String cacheName, NamedCacheConfiguration config, 
     at Microsoft.ApplicationServer.Caching.DataCacheFactory.CreateNewCacheClient(DataCacheDeploymentMode mode, String cacheName,  
     at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetCache(String cacheName, CreateNewCacheDelegate cacheCreationDelegate, 
     at Microsoft.ApplicationServer.Caching.DataCacheFactory.GetCache(String cacheName)
     at eLearnWebRole.Test.Page_Load(Object sender, EventArgs e) in D:\Asp.Net\eLearnWeb\eLearnWebRole\Test.aspx.vb:line 11
     at System.Web.UI.Control.OnLoad(EventArgs e)
     at System.Web.UI.Control.LoadRecursive()
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    

> So based on the above exception we reviewed the ‘Microsoft.ApplicationServer.Caching.Client’ binary location and noticed that reference was loaded from project bin directory instead of from NuGet package directory, so able to fix the problem after referencing the binary from NuGet package directory [The same fix will be applicable if you reference the binary from Windows Azure SDK folder ( ….\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\.. ]

RESOLUTION

The session related exception was caused due to not referring appropriate version of cache binaries. So to fix the issue, please make sure you’re referring required Cache binaries either from NuGet Package Directory [ …\<project name> \packages\Microsoft.WindowsAzure.Caching.2.2.0.0\lib\net40-full) ] or from the latest SDK folder [ ….\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\.. ]

It is recommended to go through below mentioned Windows Azure Cache articles :