Reputation: 1633
Very often I have to send data to and from third party accounting systems from asp.net sites using SQL server. The data imports usually take awhile and I'm trying to find the best method to handle the long running tasks with the ability to update the user if they want to see how it's doing. My questions are what are some of the pros and cons with the methods below and what do you recommend and why?
Old style asp response.write: I actually use this quite a bit and at the moment is my go to method because it's fast. I haven't really had many problems with it. I thought the app pool recycling would be an issue but I don't think it will recycle while in the middle of a request.
Using an IFrame like proposed here http://encosia.com/easy-incremental-status-updates-for-long-requests/: Similar to the above method except this is asynchronous.I'm not sure if this would allow an asp.net restart
A separate windows app that calls a web service from the site: I have used this method but it takes a little longer to set up and will stop running when the app pool recycles. I will probably not use this method
Windows Workflow: Have not used it but seems like(especially for small data updates) would be overkill
Windows Service: Same as above. Seems like it would be too difficult
Ajax, Separate thread and timer to poll cached results: I've used this quite a bit as well but this will stop if the app pool recycles as it's on a separate thread that asp.net isn't aware of.
Upvotes: 2
Views: 1832
Reputation: 3414
If the job takes more than a few minutes, I would recommend the job be added to a database where the status of the job could be updated by the process doing the work. You could implement a small command line application or Windows service (they really aren't too bad to make, see Writing a Useful Windows Service in .NET in Five Minutes) that monitors the database for new jobs. When it finds one it picks it up and updates its status as it goes. The end user can be redirected to a page that lists each job and its progress. When the job is complete the database row is updated and the application waits for a new job to come along.
Upvotes: 1
Reputation: 157
It's really a question of how critical is that background task you wish to perform and what triggers it. if its a recurrent event that happen in a per-known interval I find that creating a Timer on the application thread (global.asax) is best like the following example:
Implementing Recurring Background Tasks In ASP.NET - http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
To cover the client side an iframe can do the trick but Ajax request will be a bit more elegant.
as long as you sign your task on HostingEnvironment.RegisterObject, pool recycling will not intrude your job.
Upvotes: 0