I have a ASP.net web application that checks the status of my servers, it then wraps all this information up and puts it in a email. My Question how do I run this automatically say every day at like 2:00am, or like every 12 Hours?
Thanks
The best solution is to create a simple MS Windows Service which will do this job.
You'd better implement this as a separate process from your ASP.NET application. Phil Haack has summarized the reasons in this blog post. A Windows service for example or even a console application using the windows scheduler could work just fine for this task.
You want a scheduler - I recommend Quartz.NET.
As others have said, your code doesn't have to be in a web app.
If it is, then schedule a job that uses WebClient to make a request to your web app.
Check out WebDriver.
It's intended as a test / qa framework, but there's nothing stopping you from using it in a console application, which you can then run as a Scheduled Task. Note that whatever machine runs the Scheduled Task has to have a browser that WebDriver can fire up.
The easiest solution would be to create a scheduled task on your server using the Windows Task Scheduler and setup this job so that it uses internet explorer to visit your webpage.
If you open the task scheduler and create a new task. In the "Run" field put:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE""http://www.yoursite.com/yourpage.aspx"
Then in the "Start in" field put:
"C:\Program Files\Internet Explorer"
Now configure this task to run at 2:00am every day.
Related
I'm new to this but I have a question: what is the best way, to run a function / process as background in C# ASP.NET MVC ?
For context, I'm creating a website for a project that does monitoring through snmp. The thing is that the various alerts have different timeouts between them. I want to go through all of alerts and see if the time between the last check and now is already passed. If so, the program will do another monitoring for that alert. It is supposed to run as background so it doesn't affect the rest of the site.
Can someone help me?
Thanks in advance ^^
Check this great article here: https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
TLDR: you have built in and third party options.
The built in is not as polished and with as many offerings as for example hangfire
The built in tries to delay the app pool recycle to finish the job.
Hangfire has good connectivity with redis and other persistence options
Both are not suitable for long running jobs, especially longer than the application pool restart period.
Both need some recovery mechanism in case the task is interrupted abruptly.
You can build an application running on a scheduler or a windows service or a linux cron job or even try something on the cloud as web jobs. Those are not related to the iis lifecycles and have none of the drawbacks mentioned above.
I was wondering if a web application for ASP.NET supports a scheduling feature similar to Cron jobs on a Unix-like platform?
PS. I'm coding in VS2010 using C#, and I need this feature to run C# script on a configurable schedule, based on date & time.
This sounds like a windows service project type.
You coluld use a web project as an editor, to configure various parameters, and persist them to a db or file, and then read them from the windows service.
Using a web app to execute some task at different intervals is not the best ideea.
While this could be done in theory, maybe you can split it in 2projecs: 1 web app that saves config params and maybe diplays the task status, and 1 windows service that reads the params, executes the tasks and maybe updates its status.
Why not just use the Windows Task scheduler to schedule the C# script? Do you not have admin privileges?
I use cron to kickoff my scheduled jobs on unix servers, and the Windows Task Scheduler to run them on Windows servers.
We have a commercial product that may be exactly what you're looking for. JAMS is an enterprise grade task scheduler and it includes ASP.NET controls that you can drop onto your web pages. We have controls to schedule, monitor and manage tasks as well as querying past history.
And, our .NET class library can be called from C# (or any other .NET language)
If you're interested, more information is available at:
http://www.JAMSScheduler.com
I want to Scheduling in Asp.net
I have following options to implement this
To write SQLServer JOB(I dont want to do this.Dont want to go outside of .Net environment)
Second option is I will write windows service and this window service will call asp.net
webservice then this webservice calls asp.net method
(I also dont need to do this because my hosting provider might not be allow me to install
window service)
Third option is I call my scheduling method in Application_Start event in global class
(Drawback is, webserver will kill thread any time )
To call Scheduling Code in Page_Load event of Home Page(Might be nobody visits my website for hours
,Also page execution might be slow due to scheduling code)
I also found some online services that calls your page at given interval,some are given below
http://www.cronservice.co.uk/new/
http://scheduler.codeeffects.com
Anybody give me bettor solution of this and also explain why it is bettor?
Thanks in Advance
The ASP.NET application isn't the right place to implement scheduling. I would suggest creating a service or a scheduled task that runs in short intervals.
You don't have many options in a shared hosting environment. My host (WinHost) allows remote access to their database, so I was able to create an executable that ran on a local server with Task Scheduler.
The performance isn't great since the database is accessed over the internet, but it's still better than attempting to run pseudo scheduled tasks with ASP.NET.
Some hosts also offer a service that will request a url within your site on a scheduled basis. However, this didn't work for me because the task I had to run took longer than the request timeout.
There is no one solution that fits all. SQL jobs and windows jobs (scheduled thru windows task scheduler) are very widely used. In one of my previous work places they had jobs that ran on multiple platforms (mainframe,windows,sql server). Failure in some of these jobs, would cost in thousands by the day. So they employed something called ESP. This software monitored jobs on all platforms and sent a message to the control room in case of a failure.
If you throw some more light on the requirement, we might be able to help you better.
ASP.NET is not the right place to house your Scheduled Tasks. I'm using Quartz.net when I have to create Scheduled Tasks.
Create a page that launches your task and place it at the URL http://www.mydomain.com/runtask.
Create a scheduled task on your home PC that sends a request to http://www.mydomain.com/runtask.
You'll need to keep your home PC on all the time.
Ideally I would go with number 1 as you get full control/history/error reporting etc. You can write an SSIS job in .NET and have SQL server schedule it.
However, I have had a similar problem with shared hosting that is very restrictive. What I did was create a page which runs the process on page load (using validation in the querystring for security). I then used a machine I have which is always on to schedule a Windows Task Scheduler (this is part of Windows as standard) to call a bit of VB script that opens the browser and then shuts it.
So, this question has been asked lots, and i have seen many different answers, but nothing finite or absolute for my scenario.
_
What I want to do:
We have a website, with a community of users.
In the admin section of the website, there are buttons to run the following functions:
Email all the users our weekly newsletter [Thursday 4pm],
message users that day's information [Daily 6pm],
post to facebook through facebook connect [Daily 8am and 6pm],
etc, etc. (There will be new requirements coming soon too, but they will follow the same principal)
_
All I want to do is to run these functions automatically, so a member of staff does not need to go to the website, login, then click each of the buttons at the set times.
That is, effectively, have the server click these buttons automatically, at the set times mentioned above.
_
I have seen suggestions for building a service with a timer built in, which will call each function, or use windows task scheduler, build an exe / com etc, but i get the gut feeling it should not be this complicated.
We have the code already written to actually do the tasks in a .net web page, and using some custom built classes etc.
I just do not know how to automatically call these functions at the desired times.
_
Server Info:
Dedicated server running Windows Web Server 2008 R2 (64bit)
Development Enviro:
Visual Studio 2010 SP1, using .NET 4.0
_
Thank you in advance for your help.
Kindest Regards,
Del
Splitting out the code into separate exes/dlls that can be called from the Windows task scheduler as well as your application is the way to go.
There's no sense in re-inventing a scheduler inside your application when one exists in the operating system you are running on. Particularly as your application is a web application which should be doing web things not server things.
The added advantage is that you have made your application more modular and easier to upgrade.
By definition, web servers respond to requests. This fundamental design manifests in all kinds of ways, such as application pools being shut down after a period of inactivity. For this reason, it is not a good idea to execute time-dependent code inside the context of a web server, because it is difficult to guarantee that the code will actually run (without jumping through a lot of hoops that have other negative side effects). Instead, an external time-dependent mechanism should send a request to the web server to execute these functions.
You said that you didn't want to use Windows Scheduler or write an external exe because "it shouldn't be this complicated"; but I don't see what's complicated about having a scheduled task call a web service.
Well, though I agree with other answers cheering for windows task scheduler and that website has nothing to do with scheduling tasks (it can be the source of input parameters, not the task runner itself), you could try using scheduling library like quartz.NET to schedule and run your necessary processes in the background.
Download cURL - a command-line HTTP request utility. Use this utility from task scheduler to call your web page / web service with the specific query string required to execute your functions.
http://curl.haxx.se/
This isnt very secure - anyone that knew the correct query string could cause these functions to execute - but it gives you the general idea. In my opinion its much safer to write a console app that uses your assemblies to directly execute your functionality. Call the console app from task scheduler.
I'm building a web application that will need to import data from other database servers when it starts.
I would like to have this import done automatically at regular intervals. I would also like to be able to start and stop the import process from my web application.
What would be the best implementation for the import agent - a Windows Service? Something else?
If your web application needs to have this data in memory, you can use the Cache class.
Set it to expire every X hours, as you need and when it expires, re-fetch the data..
You could create a Windows Service that uses Quartz.Net to run the scheduled tasks.
You should not run scheduled task from your web app, since you don't have any guarantee that your web app is running. You're at IIS app pool management's mercy.
You might want to look at Best way to run scheduled tasks.
Of what I heard this looks like a description for Microsoft Sync Framework. I have just few information about it for myself but will be pleased to see you pointed into that direction.
I'm not sure about your question because you are talking about hourly syncing. When talking web applications, there can't be a nice way to do such a task. You have to create a console app or best task would be a Windows Service Process (which are easier then it sounds)?
Sync Framework Intro
http://msdn.microsoft.com/en-us/sync/bb821992
Sync Framework Tutorial
http://alexduggleby.com/2007/12/16/sync-framework-tutorial-part-1-introduction/
Sync Framework Samples
http://archive.msdn.microsoft.com/sync
And, when I'm editing the answer with links
Nice guide to create a Windows Service (and setup)
http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx
(if first time, try it on a test project before the production project)
This might be an oversimplification, but can you create a class that does all of this work using a Timer, and then in the application_start of the global.asax, create a BackgroundWorker that kicks off this process?
Your web application could then control the BackgroundWorker object, starting/stopping as necessary.