Fixing Problem with Session Handling and Caching for Frequently Using Transactional Pages
During last few months we had several problems with user session handling. For large financial transactional forms very often one user gets data that is requested by another user. At first shot it seems that one user session is overlapping with other. But very soon we figure out it is not like that. The problem lies inside the caching mechanism of ASP.Net. Therefore, we start to locate where the caching causes problem for us and start eliminating them.
Here I have summarized the different caching scenarios and the ways to disabling them (yes now I want to disable Cache!).
Well, in ASP.Net here are three type of caching:
1. Page Caching
2. Object Caching
3. Kernel Caching
To handle the page caching to none (do either one of the following):
· Change at the page directive
<%@ OutputCache Duration="1" VaryByParam="None" %>
· Set the value at web.config
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="MyPageCache" enabled="false" duration="1" varyByParam="None" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Finally change to page directive:
<%@ OutputCache CacheProfile="MyPageCache" %>
· Change to the page code:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(0));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetAllowResponseInBrowserHistory(false);
To handle the object caching:
Object caching usually does not create such problem if your code is smart enough. You can do regular code review so there is not any chance of session problem due to object caching.
To handle the kernel caching:
Modify the web.config file
<httpRunTime executionTimeout="60" enableKernelOutputCache="false" />
Up to this point we have disabled all type of caching that can create the problem.
Still there exists another problem that causes the Application to restart. It is either for your virus guard that is continuously checking the wwwroot or for anything else causes your application root directory to modify and restart the application. This will kill all your live session and user will be logged out or see the yellow screen of death.
The solution is easier than the solution of caching problem. Do the following steps:
· Configure your antivirus software so that it does not scan .asax and .config files.
· Contact the antivirus software manufacturer for instructions.
· Configure your Web application to store session data out-of-process.
To have an outproc session you do not need a physical server to do so. A little trick will help as follows:
<sessionState mode="StateServer" cookieless="false" timeout="30" stateConnectionString="tcpip=127.0.0.1:42424" />
The loopback of 127.0.0.1:42424 works perfectly J
Happy ProgrammingJ