Please navigate to the bottom of the page for Table of Contents

Saturday, May 28, 2011

ASP.NET Session modes explained

One of the favorite interview questions when interviewing for a web developer jobs is on ASP.NET session state. In fact, the lack of knowledge about session state providers is a very big negative when you are applying for jobs that deal in high traffic sites running on multiple servers or web farms. One of my favorite questions is on what are the different session state providers in ASP.NET?

The answer is quite simple if you have ever added the <sessionState> element in a web.config:

image

ASP.NET session state supports several different storage options for session data. Each option is identified by a value in the SessionStateMode enumeration. The following list describes the available session state modes:

  • InProc mode, which stores session state in memory on the Web server. This is the default.
  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
  • Custom mode, which enables you to specify a custom storage provider.
  • Off mode, which disables session state.

In Process session mode


In-process mode stores session state values and variables in memory on the local Web server. This is the simplest of all settings and will fail to work in a web garden or web farm scenario.
<sessionState mode="InProc" 
timeout="20"
cookieless="false">


State Server mode


StateServer mode stores session state in a process, referred to as the ASP.NET state service, that is separate from the ASP.NET worker process or IIS application pool. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.


To use StateServer mode, you must first be sure the ASP.NET state service is running on the server used for the session store. The ASP.NET state service is installed as a service when ASP.NET and the .NET Framework are installed. The ASP.Net state service is installed at the following location:
systemroot\Microsoft.NET\Framework\versionNumber\aspnet_state.exe



To configure an ASP.NET application to use StateServer mode, in the application's Web.config file do the following:



  • Set the mode attribute of the sessionState element to StateServer.

  • Set the stateConnectionString attribute to tcpip=serverName:42424.

<configuration>
<system.web>
        <sessionState mode="StateServer"
stateConnectionString="tcpip=SampleStateServer:42424"
cookieless="false"
timeout="20"/>
</system.web>
</configuration>


Note that objects stored in session state must be serializable if the mode is set to StateServer or SQL Server (described below).

 

SQL Server mode


SQLServer mode stores session state in a SQL Server database. Using this mode ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.


To use SQLServer mode, you must first be sure the ASP.NET session state database is installed on SQL Server. You can install the ASP.NET session state database using the Aspnet_regsql.exe tool.


To configure an ASP.NET application to use SQLServer mode, do the following in the application's Web.config file:


  • Set the mode attribute of the sessionState element to SQLServer.

  • Set the sqlConnectionString attribute to a connection string for your SQL Server database.

<configuration>
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Integrated Security=SSPI;data
source=SampleSqlServer;"
/>
</system.web>
</configuration>

Storing user session is a simple yet powerful concept that you should know in depth before you go for a web developer interview. Even if you have not used all the different modes, it is imperative that you learn about them and play with them. Hopefully, this article has given you enough to start with.

32 comments:

  1. This is fabulous Man :) Thanks

    ReplyDelete
  2. Thank u ! really very Helpfull for the information

    ReplyDelete
  3. cool , Thanks a lot.

    ReplyDelete
  4. I liked it. How to use custom mode.

    ReplyDelete
  5. very nice information

    ReplyDelete
  6. Thanks for sharing informative article. Download Windows 7 ultimate for free from getintopc. It helps you to explore full functionality of windows operating system.

    ReplyDelete
  7. I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. https://www.pageview.com/ Cool you inscribe, the info is really salubrious further fascinating, I'll give you a connect to my scene.

    ReplyDelete
  8. You ought to basically fantastic not to mention solid advice, which means notice: Russian Escort in Jaipur

    ReplyDelete
  9. This is the type of information I’ve long been trying to find. Thank you for writing this information. buy 50 likes instagram

    ReplyDelete
  10. There has been a rise in the demand of data science as a subject in the universities, but unfortunately, there is not a particular curriculum which can be followed in this field since it is a very generalized field. data science course in hyderabad

    ReplyDelete
  11. Thank you so much for sharing your brilliant thoughts with us. Visit Ogen Infosystem for professional Web Designing and SEO Services in Delhi, India.
    Website Designing Company in Delhi

    ReplyDelete
  12. Thanks for the information. You have discussed an interesting topic that everybody should know. Keep posting these kind of posts.
    Website: Imitation jewellery manufacturers

    ReplyDelete
  13. Thanks for sharing this nice information, Visit Webocity is website development company in delhi ncr , Best Website designing company in Delhi, We Offer Best Digital Marketing services in Delhi.

    ReplyDelete
  14. It is really a great information posting and i was searching for the same and have been satisfied after reading it,thanks for sharing new fantasy app

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. I read your article it is very interesting and every concept is very clear, thank you so much for sharing. AWS Certification Course in Chennai


    ReplyDelete
  17. This is really a good article. The Government of Ukraine grants the e-visa online. Do you have any idea about the Ukraine visa fees?. The Ukraine visa fee is dependent on your nationality and type of visa.

    ReplyDelete
  18. I continuously continue coming to your website once more simply in case you have posted new contents.
    experience design agency

    ReplyDelete
  19. Amazing post…. Travelers from around 169 countries can now apply for an online Indian visa application with 1 month, 5 years validity of 1 year. Travelers can opt for double or multiple entry, depending on the validity of the visa.

    ReplyDelete
  20. Need professional WordPress Web Design Services? We're experts in developing attractive mobile-friendly WordPress websites for businesses. Contact us today! https://just99marketing.com/wordpress-web-design

    ReplyDelete
  21. Your articles are so good to read thanks for sharing.
    Umrah Packages 2023

    ReplyDelete
  22. They not only help candidates prepare for technical interviews but also enhance problem-solving skills Are you having trouble with academic projects, essays, or writing intriguing articles? Our devoted team of specialists is ready to assist you with high-quality writing services that meet and exceed your expectations. editing help cheap writing academic essays

    ReplyDelete