Thursday, December 4, 2014

Disadvantage of enabling Session State in all ASP.NET pages

Did you even think of disabling the ASP.NET session to improve the performance of your web page? Or Do you think enabling session in all the web page has any performance impact to your ASP.NET web application. All your such questions will be answered here today.
As you are aware session data is stored in the server if you use inProc mode. So if you have huge data in session all your server resource will be utilized. So the first thing is make sure whatever you are writing into the session is really required to be stored in session and clear the session as soon as you use the data from the session.

Also just think you have a page where you are not writing anything into session in such case why you have to give write access to session. You can easily make the session state readonly in such pages. We will see a good example below for this.
In general, if two simultaneous requests come from the same browser client, they will be processed serially, in such cases if you have a page writing into a session and in other page user is trying to access the session then just think what will happen to your web page. Second page will wait for the other page to complete session writing, and after that only page will be processed. In such cases if you don’t want to write anything to session then make the session readonly in that page. This will have huge performance difference.
<%@ Page EnableSessionState="ReadOnly" %>
In case, you have a page where you are not using any session variable then make sure to disable the session in that page.
<%@ Page EnableSessionState="False" %>
Another case will be, you may not use session at all in your web application, such case you can make the session disabled for your whole application by setting this in web.config like below.
    <pages enableSessionState="false" />
Another important tips with respect to session is remove empty Session_Start from Global.asax if you have any.
I believe I have covers most the aspects related to session and how you have to use the session in your page properly to improve the performance.
I hope you enjoyed reading my article related to session.
Thanks you very much!

No comments: