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.

48 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. Really Nice Blog. Thank you for Sharing. We are the best erp software providers in chennai. For more details call +91 9677025199 or email us on info@bravetechnologies.in ERP in Chennai | ERP Providers in Chennai

    ReplyDelete
  7. 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
  8. I would like to say thank you for the amazing details and concepts you are sharing in this.
    Webseiten Gestaltung Lüdenscheid

    ReplyDelete
  9. I found a lot of interesting information here. A really good post
    office 2010 professional plus key deutsch

    ReplyDelete
  10. 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
  11. You ought to basically fantastic not to mention solid advice, which means notice: Russian Escort in Jaipur

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

    ReplyDelete
  13. 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
  14. 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
  15. I always like to read a quality content having accurate information regarding the subject and the same thing I found in this post. Nice work
    GST Registration

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. This comment has been removed by the author.

    ReplyDelete
  20. 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
  21. Here is the high DA Free Image Submission Sites list in India. PPT entry is the way of submitting your internet site link in PPT kind including your key phrase.

    ReplyDelete
  22. Attractive component of the material. I just stumbled across your web site and accession capital to say that I really enjoyed your site. With just a few clicks, foreign nationals can apply for a Kenya visa on arrival from their home. Fill the form with accurate and complete information about the passenger's data.

    ReplyDelete
  23. The information you provided is very useful, thank you very much for sharing useful information with us. You can check the Azerbaijan visa requirement online. You can apply for a visa & pay Azerbaijan visa fee in just 5 minutes.

    ReplyDelete
  24. 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
  25. I continuously continue coming to your website once more simply in case you have posted new contents.
    experience design agency

    ReplyDelete
  26. 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
  27. I really like your site it’s very impressive. Thanks for the inspiration! I really appreciate people like you!
    website: Smoking Jackets

    ReplyDelete
  28. We believe in providing a top-quality digital marketing agency in Jacksonville, FL, businesses can trust with their digital growth. You can get the most out digital marketing agency jacksonville

    ReplyDelete
  29. 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
  30. Your articles are so good to read thanks for sharing.
    Umrah Packages 2023

    ReplyDelete
  31. love to read your blogs thanks for sharing.
    Ajwa dates

    ReplyDelete