What is the best practice for starting a maintenance process on ASP.NET?

Given the ASP.NET application, I need to run the maintenance process regularly (daily, hourly, etc.).

What is the best way to accomplish this without relying on an external process, like a scheduled task on a server (suppose I don’t have access to a shared hosting server environment).

+7
maintenance
source share
5 answers

Here's how StackOverflow does it:

private static CacheItemRemovedCallback OnCacheRemove = null; protected void Application_Start(object sender, EventArgs e) { AddTask("DoStuff", 60); } private void AddTask(string name, int seconds) { OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved); HttpRuntime.Cache.Insert(name, seconds, null, DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, OnCacheRemove); } public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r) { // do stuff here if it matches our taskname, like WebRequest // re-add our task so it recurs AddTask(k, Convert.ToInt32(v)); } 

Details: http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

+11
source share

One way to do this, if you do not need to do it at the scheduled time, but just need to clear it β€œfrom time to time,” is to create a function in your Global.asax Session_OnEnd () program that will create a random number from 1 to 100, and if the number says 50, you will complete the maintenance task.

You can decrease the "100" so that the task runs more often.

There is also an article entitled "Simulate a Windows Service Using ASP.NET to Run Scheduled Tasks" at http://www.codeproject.com/aspnet/ASPNETService.asp , which uses an expiring cache to simulate a timer. He claims that it can be run on any hosted site.

If you are using the latter, read this comment from a post about this technique:

You need to be careful about the length of the task. Each new Task is a new work topic and theres a limited number of those - how does it "borrow" a stream from a managed pool of threads.

Starting with version 3.5 of the Framework, the maximum number of threads has been increased 10 times from 25 to 250. But theres now logarithmic launch of them, since it reduces the number of threads with them it becomes boring. If you run from available threads into a pool of managed threads - your answer will be time passing through the roof.

What are you actually writing here? messaging / queuing system.

If you are doing something like updating the cache, then by all means - a hit with a new task. If you are doing something like downloading a secondary HTTP resource or doing some heavy database work, write Windows Services and use a queue that allows you more control over how much you β€œbite” each time.

+2
source share

While the Cache solution works for simple cases, if your planning always changes, you're out of luck. Instead, you can use Quartz.NET , a port of the popular Quartz java framework, which is very flexible.

+2
source share

Although the way StackOverflow does this is definitely unique, you can also keep track of this question as it relates.

+1
source share

it is an external process, and I don’t know how reliable it is, but you can install something similar to a machine, which, as you know, is always on www.webcron.org .

Basically, what he does is getting to the page you are requesting according to the schedule you are requesting.

Essentially, you could click on a page on a regular schedule to run your maintenance task.

Jeff and Joel have also discussed similar things in a recent podcast using other methods.

0
source share

All Articles