Schedule HTTP Request in Asp.net site - c#

Every night my HTTPContext.Current.Cache is cleared. I want to Warm up the site during the night at time X and fill the cache with data.
Since I use HTTPContext, this has to run within the website. I am currently starting the caching by a simple button click on the site that then asynchronously fills the cache in a few minutes time frame.
I have searched for a solution where the options didn't really do the job as easily as I think it could be:
System.Timers are polling constantly. It also doesn't seem to have an option to set a specific time I want it to run?
Quartz seemed very overkill, but could probably do the job. Although a bit to read into to get it working.
RegisterWaitForSingleObject Could also work, but only a timer here as well? Also not sure if you can check if one object is already created and therefor risk of creating many of them. Nor change the timer after the object is created.
Skimmed through them relatively fast, so could be wrong. What's your take on running scheduled http request from within the site?
[EDIT] typo.

Here's what I'd do-
Create a page within the site with anonymous access, and put the code to start the caching (which is written on button click right now) ON THE PAGE LOAD event.
Create a simple console application in C#, which makes HTTPRequest to that page (You can see examples for System.Net.WebClient).
Schedule the .exe of this console application to run at certain time (like 4 AM) on Windows Scheduler on the server.

Related

Server side project C# asp.net

i have full access to server i want to run C# code on server automatically and every minute it should repeat. Is there any techniques or web services allow allow asp.net project regarding same?
I cant able to find correct reference. Post a link here if possible. I have ready code that allows my work on Page_Load. But I want to make such like , whole code should be Running every 5 minutes.
RFID #Attendance #RealTime
Similar question to what you have asked.
Best way to run scheduled tasks
All of my tasks (which need to be scheduled) for a website are kept
within the website and called from a special page. I then wrote a
simple Windows service which calls this page every so often. Once the
page runs it returns a value. If I know there is more work to be done,
I run the page again, right away, otherwise I run it in a little
while. This has worked really well for me and keeps all my task logic
with the web code. Before writing the simple Windows service, I used
Windows scheduler to call the page every x minutes.
Another convenient way to run this is to use a monitoring service like
Pingdom. Point their http check to the page which runs your service
code. Have the page return results which then can be used to trigger
Pingdom to send alert messages when something isn't right.
You have many options:
1- put the code in a web service, and create a consumer windows service to invoke it every N minutes.
2- put your code in a windows service and execute it from a timer.
3- Make a thread in the web-page (Not recommended) to execute your code.
Hope that helps you

Keep MSSQL database (application pool) responsive C#

Firstly let me apologise, as I don't really know how to phrase the question.
The issue I'm having is trying to keep my database 'alive' while users come to my site. An example being, if I build my c# asp.net application and publish it, then try and navigate to it, it takes a while to respond (which I get, I understand it, this isn't an issue for me) the problem is if some person hasn't been to the site for a while, it seems to take a while again, like a session timer has passed, I'm not sure if this is something to do with App Pool recycling?
I've tried to run a scheduled task to hit the database (trying to keep it responsive) every 15 minutes, but this doesn't seem to work, it works well every 15 minutes for say 5 hours, and then I receive a message on a random call that the request has taken over 4 seconds to respond and therefore fails.
My question then, how do I keep my connection to the database / the site responsive so that each time a person requests it, the site loads quickly, rather than having to 'start up'
Kind regards as always
I suggest to increase connection pool size in your connection string.
This looks like what you want:
Keep an ASP.NET IIS website responsive when time between visits is long: Keep an ASP.NET IIS website responsive when time between visits is long
You might consider IIS application auto-start?
Some web applications need to load large amounts of data, or perform expensive initialization processing, before they are ready to process requests. Developers using ASP.NET today often do this work using the “Application_Start” event handler within the Global.asax file of an application (which fires the first time a request executes). They then either devise custom scripts to send fake requests to the application to periodically “wake it up” and execute this code before a customer hits it, or simply cause the unfortunate first customer that accesses the application to wait while this logic finishes before processing the request (which can lead to a long delay for them).
ASP.NET 4 ships with a new feature called “auto-start” that better addresses this scenario, and is available when ASP.NET 4 runs on IIS 7.5

Cron job asp.net

I have a clockIn & clockOut module that records start time & end time for workers from an Asp.NET app.
I want to ask how to do cron job for asp.net application to see if a worker has not entered their time on that date or week and send an email notification to remind them to enter start time and end time.
ASP.NET is the wrong tool for the job. You would be better off either writing a Console Application that is run on a scheduled task, or a Windows Service that polls on a regular interval. ASP.NET is purely meant for a request/response model.
Either one can access whatever data store the website is reading from/writing to just as easily as the ASP.NET site can.
If you've done neither, the Console Application is by far the simplest to write and implement. Windows Services aren't all that bad, but involve extra overhead, including difficulty debugging, and the need to go through a special installation process as compared to the XCOPY deployment model possible with Console applications.
If you REALLY want to do it in ASP.NET, you can write an asp.net web page that does this, and use the Windows Scheduled Task to run it. The Scheduled Task can open up Internet Explorer to a specific page just as easily as it can run any executable. But I wouldn't recommend it. You'll forever have to close the IE window when the task is finished, and it's just really a "hackish" solution. I did it back when I was a pure web developer and didn't know any better, but not since.
Jeff Atwood posted on the Stack Overflow blog, titled as Easy Background Tasks in ASP.NET, in the early days of this site about a simple way to do background tasks in ASP.NET. If your tasks are relatively lightweight, this might not be a bad way. If you have a reason to really want to keep everything inside an existing ASP.NET app, this might be the way to go.
The gist:
At startup, add an item to the HttpRuntime.Cache with a fixed expiration.
When cache item expires, do your work, such as WebRequest or what have you.
Re-add the item to the cache with a fixed expiration.
Although its not technically a cron job, from within a website you can get the website to call a set page at X time of the day.
I have done this a few times via a web method call, which is called on the timer from the application start.
Basic explanation is here:
Call a webpage from c# in code

How to run background service on web application - DotNetNuke

I made dnn scheduler and set to run it on every 1 min. It works when I do something on site. But I need to run some actions when I am not on the site. For example insert record to database with currenct time. Is this possible?
In Host Settings, use Scheduler Mode = Timer Method
This will make the scheduler run in a separate thread that is not triggered by page requests.
If the scheduler runs in the timer method, it won't have access to the current HttpContext.
You will also have to make sure that DNN is kept alive, and IIS doesn't shut down the application due to inactivity. Setting the application pool idle timeout appropriately, and pinging the /Keepalive.aspx should take care of this. Nevertheless, using the DNN scheduler for critical tasks is not a good idea.
See Also:
Creating DotNetNuke Scheduled Jobs
DotNetNuke Scheduler
Explained
If you just want database related things, such as inserting a record, you can use database jobs. You didn't mention what dbms you use but almost every database have almost same functionality under different names.
Doing the equivalent of a Cron job is still a pain in the butt on Windows.
The DNN Scheduler will work if you aren't super concerned about when it runs. What you may need to do is have more logic on your end... if it only runs every 10 minutes, or every hour or whatever you may have to look at your database tables, determine last time it ran and then do whatever it needs to do to 'catch up.' In your case add 60 minutes of info versus every minute.
I'm struggling to think of an example of why I would just write to a table every minute or on some interval. If I needed it for a join table or something convenient to report off of you should generate them in larger chunks.
The other option is to write a small .NET windows service which isn't that hard and have it run every minute. That would be more reliable.

ASP.NET script "scheduling" question

I am looking for a way to have a script run every day at 5am to delete the contents of a Temp folder. The following is the method I am considering. I would appreciate any thoughts on this or suggestions for other methods. I want to keep everything local, so that there are no external dependencies from outside my account on Discount ASP hosting.
Have a textfile containing the datetime of the next desired run (5:00am tomorrow).
Have a Datetime cache value that expires after (one hour?)
When someone hits the website and the cache is expired, reload the datetime into cache
If the Datetime has passed, run the script to be "scheduled" and add 24 hours to the DateTime in the file
Your comments are appreciated.
You are on the right way. Here is a good article how to achieve this.
Also, based on your comment, why not use session end event to purge? Also, you can hook to application end as well, just in case.
You could also create a web service to perform the task, then have a scheduled task call the web service periodically.
This isn't a good idea, as you are depending on input that might never come to execute an operation which has a need to be performed on a regular basis.
Because of that, you need input from outside of the site (since the site is triggered by requests) to trigger your event, in other words, a scheduler.
You should be using a Scheduled Task for this. If you aren't, then you should have some other process which will send the event to the site (expose a web method perhaps) which on the schedule you desire.

Categories