cagin
cagin

Reputation: 5930

How to give a web request to windows task Scheduler

I have a static webmethod i.e (http://localhost:61176/trunk/MusteriKontrol.aspx/CheckMusteri) I want to call this method from Windows Task Scheduler. How should I do this?

Upvotes: 0

Views: 2708

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25057

You could use a PowerShell script. This has a check for the time, a commented-out Try..Catch in case you want to do something if an error is raised from the query, and records when it has run in the Application event log:

# Download the HTML of a web page.
# Make sure an event log source is created with New-EventLog -LogName Application -Source MyPSscript
# Only do this if the time is 5a.m. or later, to give the server a rest from midnight.
$currHour = (Get-Date).Hour
if ($currHour -ge 5) {
    $web = New-Object Net.WebClient
    #try {
        $temp = $web.DownloadString("http://localhost:61176/trunk/MusteriKontrol.aspx/CheckMusteri")
    #}
    #catch {
        # do nothing.
    #}
    write-eventlog -logname Application -source MyPSscript -eventID 1001 -entrytype Information -message "Fetched web page." -category 0
}

Upvotes: 1

László Koller
László Koller

Reputation: 1159

Have considered a different alternative to scheduling the call of a web method from Windows Task Scheduler?

For example, scheduling tasks from within an ASP.NET project is possible using the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your web application. In your case, you would schedule a callback that would call your web method at a specific time. Revalee works very well with tasks that are discrete transactional actions, like updating some database values or sending an automated email message (read: not long running). The upside is that the code that schedules the callback, as well as the code that performs your action (i.e., your web method), would all reside within your app.

To use Revalee, you would:

  1. Install the Revalee Service, a Windows Service, on your server. The Windows Service is available in the source code (which you would compile yourself) or in a precompiled version available at the Revalee website.

  2. Use the Revalee client library in your Visual Studio project. (There is an MVC-specific version too.) The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet.

  3. You would register a future callback when your code calls the ScheduleWebMethodCallback() method (this example is assuming that you need your action to run 12 hours from now).

    private void ScheduleWebMethodCallback()
    {
        DateTimeOffset callbackTime = DateTimeOffset.Now.AddHours(12.0);
    
        // The callback should be in 12 hours from now
        Uri callbackUrl = new Uri(string.Format("http://localhost:61176/trunk/MusteriKontrol.aspx/CheckMusteri"));
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    }
    
  4. When Revalee calls your application back, your app would perform whatever action you have coded it to do in the web method you have listed above.

In case it was not clear above, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network. It resides and runs on a server of your own choosing, most likely your web server (but this is not a requirement), where it can receive callback registration requests from your ASP.NET application.

I hope this helps.

Note: The code example above uses a synchronous version of ScheduleCallback(), the Revalee client library also supports asynchronous calls à la:

RevaleeRegistrar.ScheduleCallbackAsync(callbackTime, callbackUrl);

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.

Upvotes: 0

Related Questions