user3544117
user3544117

Reputation: 605

ASP.NET MVC session variables timeOut

I have a ASP.NET MVC website that need to run on a computer 24h/24

I use session to store variables that I get through a configuration file or an external API:

HttpContext.Session["Value"]

In my web.config file, I set the session timeOut to maximum :

<system.web>
  <sessionState timeout="525600" />

The website need to be displayed for a long time and still have the session variables when I click on a button. (maybe 1-2 day later)

Will it work like that ? Is there something to do in the IIS configuration too?

I ask because it's difficult to test it...

Upvotes: 1

Views: 3963

Answers (2)

avidenic
avidenic

Reputation: 643

Well if you are using inproc session, then you will need to go to IIS and set the application pool recycling on a long interval (or disable it), the same with idle time (either set it to 0 to always run or implement a pinging service in your web page).

The best option would be, if you store your session variables in a persistent storage (either DB, or file or separate process).

You can do that by tinkering with web.config (this is a sample config)

<sessionState mode="SQLServer"      
  regenerateExpiredSessionId="true "
  timeout="525600"
  sqlConnectionString="Data Source=MySqlServer;Integrated Security=SSPI;"
  stateNetworkTimeout="30"/>

Read more about it here and here.

There is also a question of: why do you want to do this? If you want to save user data for longer period of time it might be worth looking into HTML 5 local storage.

The third option would be that the server is stateless and all data is stored in javascript variables. The "bad" is that you will need to send everything related upon each request, the good thing is, that page will "work" for as long as browser window is opened.

Relying on session variables for longer periods of time can lead to bugs which are difficult to reproduce and bad user experience because page stops working for no apparent reason. Try to avoid it, you will just create unnecessary problems =).

Upvotes: 2

Marian Ban
Marian Ban

Reputation: 8188

You can set the application pool to alwaysRunning mode and idle timeout 0:

enter image description here

but the application pool will be still recycled (and session restored) when you deploy new application (change web.config, bin folder content) or restart the server. To overcome this problem set session state mode to State Server or SQL Server mode.

Upvotes: 1

Related Questions