Scheduled tasks for ASP.NET - asp.net

How to execute asp.NET code in scheduled task of windows?

I've often used wget / curl to do exactly this from windows scheduled tasks (or even from cron on a different server). For certain things it is better to keep it all within the web application.
e.g.
curl -k https://example.com/update.aspx?id=71077345 -u username:password
or
wget -O - http://example.com/process.php

ASP.Net has good integration with Windows Workflow (WF). I've accomplished 2 project with such integration. WF allows persists tasks, so it is independent on IIS restarting or system crash

The simplest and probably the easiest way to do this is to make the code a command line app and then execute it using the Windows Scheduler found in Control Panel.

If you have full access to the server, use the scheduled tasks.
If you do not have access (shared hosting), create a webpage/service that you can call to trigger the action. Be aware that the "action" is limited in time.
You could create a small application, that you can schedule on you own computer to call the remote server, to trigger the action.

There is no way to schedule ASP.NET code from within a web application, because the web is stateless.
My preferred method to schedule .NET code, is to write a windows service that I install on the web server. See. http://blog.dogma.co.uk/search/label/windowsservice.
An quicker, less efficient, way is to add the code to a page and execute that page from Windows Scheduled Tasks.
Rich

Related

Scheduler for ASP.NET?

I want to run some of my apps code every night in some sort of task or scheduler. Is there any build in functionality in the framework? If not, whats an easy 3rd party framework to get scheduling?
Thanks
You can create a console application and run it as a scheduled task. Alternatively you could look at using Quartz.Net which is a port of the Java Quartz, a framework for scheduling jobs.
Either way your finished product will likely be a console application that is scheduled to run with scheduled tasks, or a class library as a Windows Service. ASP.NET its self won't do the scheduling.
If you can, you could use IIS 7.5 Application Warm-Up Module : http://blogs.iis.net/thomad/archive/2009/10/14/now-available-the-iis-7-5-application-warm-up-module.aspx

How to perform daily maintenance on database records in MVC2 project?

A web application works with the database. Once a day, the database should be scanned and alerts should be sent to users.
From what I've seen out there, additional project has to be created which will be installed on the server and will work with the same database. Executable created by this project has to be installed in Windows scheduler to be activated once a day.
This seems complicated and inefficient: starting additional executable and working on the same database.
Is this the best possible way to do this?
Well you have different possibilities: Windows Scheduler with an executable is a good one. Another possibility is to write a Windows Service which will execute the task in the background. Quartz.NET is a good framework for this but the Windows Scheduler might be sufficient for your scenario. One thing is for sure: it is be better to perform these tasks outside of your ASP.NET application.

start batch job from IIS

What is the right approach when users (authenticated domain admins) should be able to start batch jobs (usually exe files) from an IIS (7.x) aspx (c#) page? This is an intranet site. The batch jobs have to run on the web server as Domain Admins. The website pool is executed by network service or some similar restricted account in AD.
Approaches I can think of (and their disadvantages):
1. Start exe file with System.Diagnostics.Process.Start with another account. This feature is disabled in IIS 7.x, how do I allow it?
2. Create a sheduled task and call scheduled task-api. This unmanaged dll is giving VS compiler warnings because it's unsafe to call from managed code.
3. I suppose there's a better approach, because the previous suggestions doesn't appear safe or robust.
I would suggest that you have either a Scheduled Task or Windows Service that polls a common storage repository to see if a batch job should be run - as batch jobs are typically used for long running process.
Your could persist the deatils of which batch file and arguments you want to run from your ASP.NET website into a database (MySQL, Oracle, SQL Server, etc) and then have your Windows Service / Scheduled Task poll against this database at regular intervals.
I agree with Kane. However, if you must do...
http://www.dotnetscraps.com/dotnetscraps/post/Run-a-batch-file-as-a-specific-User-(or-Administrator)-from-ASPNET.aspx

Can my asp.net application make a call to a linux server, speficially some shell script?

If I have a asp.net web app on a windows box (obviously!), and I need to execute a shell script that is on a linux server, is that possible?
How can I do this safely?
To build on what cxfx has said above your best bet might be to set up a web server on the linux box, and build a web service using, for example, php.
php allows you to run shell scripts - so by calling a web page runscript.php and then using exec (http://php.net/manual/en/function.exec.php) you could run the script.
Security is a consideration - you could restrict access to the linux web server purely to the IP address of the server hosting your asp.net site?
Hope that helps.
One way of doing it is by logging through ssh. You can use the granados C# library for it. This will let you run arbitrary commands. If what you need is to always run the same command, you can take a different approach like a CGI script (hide it with at least HTTPS and user/password).
Your best bet might be to expose the triggering of the shell script through some sort of web interface, like a secure web service.

windows service versus asp.net Application_BeginRequest event with threads/timers

What are the advantages/disadvantages to running time based jobs using:
windows services
Application_BeginRequest to start seperate threads / timers.
One disadvantage of running the jobs in the context of a asp.net web appplication is during .net recycling things will have to be setup again, any others?
To my mind, there's no real benefit to doing time-based things in a web app. Go straight to a windows service. You know the process should be up and running all the time.
The ASP.NET site may simply unload, and will only operate again once someone starts browsing. The lifecycle is all wrong -- it's much 'choppier' than a service.
Lastly, services aren't very hard to create.
If you have administrative access to the server, I would either run a Windows Service or a scheduled SQL job depending on what you are trying to achieve.
It is nice to be able to stop/start and log these jobs independent of your web application. Also, if you have problems or errors in the job, it could adversely affect your website.
Finally, you are forcing the web application to go through code at every request to see if the timer has elapsed, which is an unnecessary overhead.
As I said to start with, the implementation depends on what the job is. If it is simply to update a number of database records, I'd use a scheduled job in SQL Server. If you need file I/O or access to external services, then a Windows Service might be more appropriate.
It is worth noting that you need to build in your own scheduling and thread safety into Windows Services. An alternative is to build a console application and use an application like FireDaemon for the scheduling.

Resources