I need to keep the session of my application alive. I have seen that it can be set to a certain time by
Sub Session_OnStart
Session.Timeout = 90
End Sub
Is there any way to make in unlimited?
Thanks While it's theoretically possible to make the timeout for none in-process session infinite, in practice it is not a good idea (in-process session is recycled when the worker process is recycled so at some point the server may decide that it needs to stop and start the worker process, thus removing the session).
The reason that it's not a great idea can best be explained by an example. Suppose that you are a high traffic site like Amazon, let's call your site Nile. Now, I visit your site and add some items to a session based shopping cart and the session is stored in SQL Server because you're being really good and you have a web farm in place. Now, I close the browser down and come back to your site and decide to add some more items. Thing is, I'm now in a new session. But wait - the old session is still in the database. There are now two sets of sessions for me - only one of which is relevant, the other one is stale. Okay, I can't decide what I want so I close the browser down and come back later on in the day. I now have three sessions - two stale and one active. The good news for you is that your site offers some of the best prices on the web so it's hugely popular - you're attracting 1 million shoppers every hour.
Can you see how staggeringly big your session data is going to grow very quickly?
If you have a requirement for this - you need to analyse what the problem is that you're really trying to overcome. I'll guarantee that it's not the one you think it is. Keeping your site communicating back with a main site via an Http Handler. I would typically use a simple setup like this:
JavaScript
function heartbeat(timeout) { setTimeout( " triggerHeartbeat" , timeout); function triggerHeartbeat() { $.get( " /Heartbeat.ashx" , null , function ( data ) { heartbeat( 600000 ); // Triggers every 10 minutes... }, " json" ); }Now you have a heartbeat that will pulse every 10 minutes (the call in the Ajax query simply sets up the next triggerHeartbeat call to happen 10 minutes later. You could replace this with setTimeout("triggerHearbeat", 600000); if you wanted to get rid of the heartbeat method (don't forget you'll have to trigger the first call).
Anyway, that's all the code you need on the client side - other than putting the initial call to heartbeat in a $.ready method. You need to create an HttpHandler at the server side called Heartbeat.ashx. The code in it is as simple as this:
C#
public HeartbeatHttpHandler : HttpHandler, IRequireSessionState public bool IsReusable { get { return false ; } } public void ProcessRequest(HttpContext ctx) if (ctx == null ) throw new ArgumentNullException(); ctx.Session[ " Heartbeat" ] = 1 ; // You can put any value in here - this merely keeps the session going. }Don't forget to add this to your list of HttpHandlers, and you're good to go. You'll find many examples on the web that follow this pattern - the code isn't complex, and it's a very well understood solution to the problem.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •