TN.
TN.

Reputation: 19820

How to keep ASP.NET session during long request processing?

The scenario is following:

  1. A user clicks to download a complex report that takes long time to complete (longer than the session timeout is).
  2. After the report is completed, user clicks on any other link, but the session is expired.

The user is wondering why the session has expired during the long running request.

How to tell ASP.NET not to expire session during the request? (To start counting the session timeout after the request is completed.)

UPDATE: The solution should work without JavaScript.

Upvotes: 2

Views: 3122

Answers (3)

Basic
Basic

Reputation: 26766

You could manually extend the session timeout for the particular session by setting the HttpSessionState.Timeout value during report generation (possibly multiple times).

This does seem a little clunky, however, assumes you'd always be able to know in advance how long to increase it by, and you'd need to make sure you set it back afterwards or you'd end up with visitors who've generated a report having different timeouts to those who haven't

While this might work, it's certainly not the best approach and you really should have this run as an asynchronous task.

Upvotes: 0

David
David

Reputation: 218877

A web application should respond to requests in a timely manner. I would argue that the actual problem isn't that the session is timing out, it's that a long-running process is happening in-line when it should be off-loaded to the back-end.

Instead of making the user wait for the report, kick off a process on the server to generate the report and respond to the user with a response indicating that their requests has been queued and is being processed, and that their report will be ready when the process is complete.

The user can then continue to navigate the website, use the application, etc. When the report is done, there are various ways you can notify the user. The application can send them an email, perhaps even with the report attached. Or there can be some in-application notification system (like the Facebook notifier) which would either have an AJAX polling mechanism or (in response to your update about not using JavaScript) could be more passive and simply notify them on their first page request following the completion of the report.

In any event, any process which takes this long shouldn't be in-line in a web application.

Edit: As a proposed setup, you could do the following:

  1. When the user requests the report, add a record to a queue table which includes any information needed to begin generating the report.
  2. Return application control to the user.
  3. Write a Windows Service or scheduled console application which polls the queue table and processes any reports in that table.
  4. As each report finishes, update its status in the table.
  5. Optionally also have the service/application notify the user (email, add a record to a table that drives an in-app notification system on the website, etc.).
  6. The user can follow the notification to download the report, or just check back their "report queue" in the website.

Upvotes: 5

Giorgio Minardi
Giorgio Minardi

Reputation: 2775

What does take long time, the generation of the report or the download of the file? If the operation take to much time you'll probably get an page timeout, your method took too long to respond.

If the session expire because the file requested from the user takes to long to download, then this is a normal behaviour. The user request the file and he looks non active until the next request, the session doesn't get refreshed and it expires.

Upvotes: 0

Related Questions