Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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
Posted

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.
 
Share this answer
 
Comments
Furqan Sehgal 22-Jan-14 11:17am    
Thanks for detailed answer. I explain you my requirement, may be you can advise me something.
I need to send an email message to user every 10 minutes. Code is in Global.asax.
As long as session runs, it works fine. When session is closed, it stops working.
Pete O'Hanlon 22-Jan-14 11:24am    
So what you're after is having the session keep itself alive as long as the user still has your page open in the browser? What you need to do is create a heartbeat that fires back to your site just to inform it that the site is open. I will sketch one out for you as a separate answer.
Peter Leow 22-Jan-14 21:21pm    
Excellent. +5!
thatraja 23-Jan-14 8:20am    
10!
It seems you forgot the 'Reply' buttons on comments, you could blame CM if those buttons're not attractive :)
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.
 
Share this answer
 
Comments
Peter Leow 22-Jan-14 21:20pm    
Excellent. +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900