开发者

How to prevent browser timeouts in ASP.NET?

开发者 https://www.devze.com 2023-03-04 12:36 出处:网络
The scenario: user uploads a small file or about 100 kB to the ASP.NET 4.0 application with perhaps 1000 units of work.

The scenario:

user uploads a small file or about 100 kB to the ASP.NET 4.0 application with perhaps 1000 units of work. The server then gets to work on the units of work, one at a time. Each unit takes a few seconds to complete due to requesting information from an external service. The results are only saved to the database if all units were completed successfully, using a transaction. Once completed, the user may get a list of what was done.

The problem is that the user does not get the confirmation, instead his browser I think gives up because of a timeout.

Future files are expected to be a few 100 times larger, increasing the problem.

I want to prevent this timeout.

Here are some ideas I had:

  1. Optimize the code to run faster. This is done and is no longer the problem.
  2. Run the requests to the external service in parallel.
  3. Increase the server timeouts a开发者_如何学C little.
  4. Let the user upload the file and then send him an email with the results later when the file has been processed.
  5. Somehow make the page refresh and show some progress information to the user while waiting, e.g., 5% complete - done in 10 minutes.

How could I implement this last step, showing progress information and preventing browser timeout?

Other suggestions are welcome.

Thanks,


You need to decouple processing the file from browser response. This is achieved by

  • Creating a persistent item (i.e. in database, file, etc) in a queue so that it is fault tolerant
  • return success result to browser
  • Create a queue worker to asynchronously process your queue.


You put the data uploaded into a queue in your database. Then, you have an asynchronous process (example Windows Service) to pull items from your queue and process them. You can update your DB with progress of each operation, and when full completed, remove the item from the queue and update your other tables.

For progress, the user can then query the queue table for the status of his upload.


An easy trick is to write a piece of javacsript to repeatedly request something from the current page. If you use a request action of HEAD then the server will only respond with a minimal amount of information each time.

Something like:

<script src="/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
setTimeout("callserver()",6000);
});

function callserver() {
var remoteURL = '/yourpage.aspx';
$.get(remoteURL, function(data) { setTimeout("callserver()",6000); });
}
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号