1

I would like my web service to call a clean up routine every X hours, with no input from any user, or any call to "Start Clean up Services". I understand I can call a method to start this service, but I want no user interaction at all. I want to publish this service, and it automatically starts, and runs my clean up process every X hours.

Any ideas?

Tizz
  • 820
  • 1
  • 15
  • 31
  • Try this solution at http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc – Tariqulazam Nov 06 '12 at 20:59
  • ASMX service, or WCF service? Is the cleanup process cleaning up the web service itself, or the database, or what? Does it have to run inside of the web service? – John Saunders Nov 06 '12 at 21:09
  • Its published to an IIS 7.0 server. The clean up process is to clean up local files that are used/created by the service that are no longer needed. Think of it as an automatic recycle bin cleaner. Items are put into a folder that arent needed, after X hours I want to check if these items are "old" (3 days) and if so delete it. – Tizz Nov 06 '12 at 22:39
  • 1
    possible duplicate of [Best way to run scheduled tasks](http://stackoverflow.com/questions/542804/best-way-to-run-scheduled-tasks) – tomfanning Nov 07 '12 at 15:30

1 Answers1

2

You can setup a timer in the Global.asax.cs file that'll go off every X hours, or you could also create a scheduled tasks that goes off every X hours as well to trigger the clean up service.

If you do not have a Global file in the project you can simply add one by adding one to the project. To do this right-click on the project -> Add -> New Item, and then in the dialog that pops up in select Global Application Class and hit Add. Then inside the Application_Start event you can initialize your timer to perform the action.

public class Global : System.Web.HttpApplication
{
    private static System.Threading.Timer timer;

    protected void Application_Start(object sender, EventArgs e)
    {
        var howLongTillTimerFirstGoesInMilliseconds = 1000;
        var intervalBetweenTimerEventsInMilliseconds = 2000;
        Global.timer = new Timer(
            (s) => SomeFunc(),
            null, // if you need to provide state to the function specify it here
            howLongTillTimerFirstGoesInMilliseconds,
            intervalBetweenTimerEventsInMilliseconds
        );
    }

    private void SomeFunc()
    {
        // reoccurring task code
    }

    protected void Application_End(object sender, EventArgs e)
    {
        if(Global.timer != null)
            Global.timer.Dispose();
    }
}

For more information on the Global file you may want to refer to MSDN

JG in SD
  • 5,427
  • 3
  • 34
  • 46
  • I do not have a Global.aspx.cs in my c# project for the web service. Can I add it or do I need to do something special? – Tizz Nov 07 '12 at 05:54
  • @Tizz Sorry I had the name of the file slightly wrong, it should be `Global.asax.cs` updated the post and edited the answer to include how to add that if it doesn't exist – JG in SD Nov 07 '12 at 15:28
  • @ JG-in-SD Thank you for explaining how to get the global file in place. It seems though that Application_End never is called, even when I publish the web service again. Any explanations? – Tizz Nov 07 '12 at 19:01
  • @Tizz I've seen the Application_End event not get called when hosted the web site in Visual Studio, if you use the stop button or shift+F5 to stop the web site it won't get called. If you right click on the icon for it in the tray and go to stop it should get called. As for as if it is hosted in IIS, again that will depend on how the web site was stopped, if it was forcibly stopped or stopped is a normal manner. – JG in SD Nov 07 '12 at 20:00