Friday 6 November 2009

ASP.Net Keeping Session Alive

ASP.Net Time-out

We have a problem with a client with a slow network that does a file upload. This takes only a minute or some on our development network. The client IIS server has a timeout of 60 minutes. However, the file upload may take longer than 60 minutes, and the page times out before the operation is complete.

Looking into the mechanics of what is going on I have found the following:

IIS Time-out is controlled by 3 settings

Note: none of these can be set to zero, or indefinite. The value for the first two is in seconds.
  • ConnectionTimeout - How long since the link between the client and server is idle.
  • HeaderWaitTimeout - I don't understand this one, but it is to do with client requests
  • MinFileBytesPerSec - This measures the amount of data received at the client.

It seems the best approach is to find a way to tell the server that our page is actually still alive.
Here are two code examples of an approach to doing this:

The common logic to each of these methods is to have a small part of the page, such as a frame, automatically refresh before the page is due to expire. So if it should expire every hour, have it refresh every 50 minutes. The question in our scenario will be - will this work when part of the page is still waiting for SQL Server.

To handle this we need to do something like threading. In other words - send the page off to do some database work, and in the meantime, continue to be responsive.

This link shows how to do that Threading in ASP.Net

To achieve this in ASP.net, as well as understanding threading and automatic page refreshing we need to know how to use frames too. This is elementary to most web developers, but I am not a web developer!!.

To create a page with two frames I actually created 3 aspx pages. This is the key part of the code that appears on the parent page.

Refreshing Frames Individually

To refresh a frame manually, you can put code like this in the click event of a button, or some other event. This is client side javascript.

parent.frames[1].location.href=parent.frames[1].location;

To refresh a frame automatically, put this line in the head section:

<meta http-equiv="REFRESH" content="15" />

(This is the on-line application I used to convert html code into something that will display properly in a blog: www.felgall.com/htmlt47.htm)

To solve the problem with the client application that we have:
1) Rename the data load form
2) Create a new form with the name of the old dataform
3) Add a frameset to the new form, the old form will be the first frame in the frameset
4) Create a new form
5) Add the new form to the frameset
6) Add the automatic refresh feature to the new form.

NOTE:
The proof of concept still needs to show that this works in conjunction with a dataload taking place in another frame, and that it is sufficient to keep the session open.
Carry on from here on Thursday.