How to use System.Threading.Timer with asp.net web service - c#

Background:
I have a .NET 4.0 web service running on Windows Server 2008 written in C#.
I would like to run a timer inside the asp.net web service that runs code once a day. I have heard of some limitations with this (see below) but I am asking if what I plan is OK in order to get around some of the limitations and to keep the code inside the web service as it is not mission critical code. However a crash is unacceptable. Note: I am not and can not use WCF.
The plan:
Declare and instantiate System.Threading.Timer in the constructor of
my web service (right next to the commented out line:
//InitializeComponent(); ).
Fire timer every 30 minutes.
Note: Because I am using System.Threading.Timer I should not have to run
keep-alive code according to msdn Tip #6 below.
IF the current time
is within 30 minutes of my database value (desired time to run code)
THEN run code.
Questions:
I would like to know how reliable it is to use System.Threading.Timer in asp.net c# code using the plan above and if it would run correctly let’s say more than 97% of the time?
Will this plan work when the app pool is recycled and when IIS server is restarted?
It seems there are issues using timers in web services with regards to:
app pool recycling
threads dying
no one hits web for a long period
of time memory issues (better to use windows service)
References:
ASP.NET Site - Firing some code at a specific time
Timer on Website to activate Web Service calls every hour
http://forums.asp.net/t/1079158.aspx/1
http://msdn.microsoft.com/en-us/magazine/cc163854.aspx (see Tip #6)
From Tip #6
“The Timer class, found in the System.Threading namespace, is a wonderfully useful, but less well-known class in the .NET Framework, at least for Web developers. Once created, the Timer will invoke the specified callback on a thread from the ThreadPool at a configurable interval. This means you can set up code to execute without an incoming request to your ASP.NET application, an ideal situation for background processing. You can do work such as indexing or sending e-mail in this background process too.”

You're using the wrong tool for the job. Web Services are meant for on-demand use, while services and scheduled tasks should be used for periodic activities. Move the relevant code out of the web service into a shared library and then either create a Windows Service with a Timer or a Console App that can be scheduled through Scheduled Tasks.

If for some reason, you must do this. Be sure to read about IRegisteredObject
as well as Haacked's post on this subject

You can setup your timer object(s) in the Global.asax.cs file in the Application_Start event instead of inside a web service.

Related

Run a function / process as background in C# asp.net mvc

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.

How to schedule a method in .net core?

I have to give a call to Web API from that Helper method which will dump required Data to database.
I wanted to create a method which will be called automatically every
hour.
Do i need to call it from Startup CS file if yes then how ?
The intended method will suppose to call API to get the data which
will be dumped in SQL database in hourly manner.
I would highly recommend Hangfire for this. https://www.hangfire.io/
It depends on how you want to do it, and which OS you use.
If you use Linux, you can use the OS scheduler - cron. Or AnaCron.
If you you Windows, you could use Windows task scheduler.
Cron will probably work on MacOS too, though I suppose they have their own task-scheduler.
If you're using your application on Azure/AWS/DigitalOcean/AppEngine, then they all probably have facilities to do this.
There is a library to do job scheduling in .NET, it's called Quartz.
I don't know if they've ported to .NET Core yet, but I suppose they have.
Also, if you're using your application inside an ASP.NET (Core) application, I don't know how this is with IIS recycling the app-pool after 20 minutes of inactivity. If it does, then you can't do it in ASP.NET or you need to change the application's or IIS's configuration.
Note: According to this, the ASP.NET Core application definitely gets stopped when IIS recycles the app-pool, even though IIS would only have to recycle the reverse-proxy.
One of the ways we have done it in the past, is having an ashx handler in the application, which does the job once (using JWT header authentication so no DOS/DDOS is possible).
Then we just have a program running somewhere that issues a get request to the ashx handler every hour. This ensures the task is run every hour, and it lets IIS recycle the app-pool. Because that means IIS potentially needs to start then entire application, you should increase the get request's timeout accordingly. That system is simple, and it works the same everywhere, dev-machine, on-premise, self-hosted, shared-hosting, azure, aws, appengine, digitalocean, etc. and it doesn't even require any library.
Best of all, in its simplest form, you can just use wget and cron, which is really simple, and you can do rather complicated stuff with that, e.g run a program at the last day of the month (28,29,30,31) at 01:05, or like every 1st friday of the month, or on every monday-saturday from 4 to 23 o'clock, every 13 minutes starting 04:23, or whatever the hell you want.
ZERO .NET programming required at all for the job scheduling.
You could set up a Web Hook in your application that call that method and create a Logic App in Azure that fires every hour to call that web hook.
It depends on a few things, notably where your application(s) are hosted ( azure, aws, on prem, locally) and how you want to couple your scheduler to the web api service.
For example, say you are hosted in azure. You can use a timer in azure scheduled every hour to trigger and invoke your endpoint.
AWS has a similar feature, and you can invoke a lambda function.
If you are on prem, you could create a service such as hangfire that does the scheduling.
And if your really looking to run the schedule manually, you could even create a WCF service with a Timer and TimerCallback that is invoked every hour.

Is it possible to have an ASP.NET application perform a timed action? [duplicate]

Once a day, I want my ASP.NET MVC4 website, which may be running on multiple servers, to email a report to me. This seems like a pretty common thing to want to do, but I'm having a tough time coming up with a good way to do it.
Trying to run a timer on a server to do this work is problematic for a couple of reasons. If I have multiple servers then I'd have the timer running on all of them (in case a server goes down); I'd need to coordinate between them, which gets complicated. Also, trying to access the database via Entity Framework from a background thread adds the complication that I must employ a locking strategy to serialize construction/disposal of the DbContext object between the periodic background thread and the "foreground" Controller thread.
Another option would be to run a timer on the servers, but to have the timer thread perform a GET to a magic page that generates and emails the report. This solves the DbContext problem because the database accesses happen in a normal Controller action, serialized with all of the other Controller accesses to the database. But I'm still stuck with the problem of having potentially more than one timer running, so I'd need some smarts in the Controller action to ignore redundant report requests.
Any suggestions? How is this sort of thing normally done?
You should not be doing this task from your web application as Phil Haack nicely explains it in his blog post.
How is this sort of thing normally done?
You could perform this task from a Windows Service or even a console application that is scheduled to run at regular intervals using the Windows Scheduler.
The proper solution is to create a background service that runs independently of your website. However, if that is not an option there is a hack where you can use the cache as explained in Easy Background Tasks in ASP.NET by Jeff Atwood.
A few options:
If you are hosting on Azure as a Website, check out WebJobs which was released recently in preview (http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/)
If you don't want the pain of extracting out your email logic outside of the website, expose that functionality at a url (with a handler, mvc action, etc.) and then run a Windows Scheduled task that hits that url on a schedule.
Write a simple console app that is executed similarly via a Windows Scheduled task.
Or write a simple Windows Service that internally is looping and checking the time and when reached, hits that url, runs that exe, or has it's own code to send you the email.
I would recommend running Quartz.NET as a Windows Service:
Quartz.NET - Enterprise Job Scheduler for .NET Platform
There's boilerplate code for a Windows Service in the download.

Scheduling in Asp.net.What is best solution?

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.

Building and controlling a scheduled task or service from a web application

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.

Categories

Resources