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.
Related
I am planning to create a service that checks something on a website with an interval. Compares the results with the DB and sends a email out if changes has been made.
No user interface is needed.
What type of service should I use for this type of application?
There's no single correct answer to your question. If you want to keep things really lightweight, you could use an Azure Function App with a timer trigger.
If the Function App model isn't right for you, you can deploy a scheduled Web Job to an App Service deployment.
For some scenarios, the best alternative is a virtual machine with cron / Windows Task Scheduler. For others, it's to write an app that comes with its own scheduler.
You can try to look at Azure Functions. That is probably the simplest thing you can do and it can run on timer. That seems quite relevant to your use case.
Azure Functions supports an event based on a
timer using Cron job syntax. For example, execute code that runs every
15 minutes and clean up a database table based on customised business
logic.
I have written a web application in asp.net. It has some user roles. There are 3 roles which are Manager, Accountant and Employee. The employees write their expenses in a form and send it to Manager. When manager approves it, it'll be sent to Accountant to pay it. I need to have an idea that when manager doesn't approve the employee's expense in 48 hours, it should send an automatic e-mail to Manager's mail.
I thought that I can write another small console application to handle that by checking every hour. But it would waste resources and decrease performance.
I need a good idea to handle that. How should I do?
There are several options, but if I were you I would go with first or second options.
Console App & scheduler
I would create that console application that every time is run perform the check for you.
Then I will have it run using Windows Scheduler in a daily basis (at 00:05) or a hourly basis if you prefer so. This way Windows Scheduler daemon will launch it every hour and the rest of the time your app is not running.
Check this Microsoft link to see how a scheduled task is created in windows.
Restful Web Service & scheduler
As suggested in #marapet answer, having a restful web service that allow you to perform this action instead of a console application would give you the advantage of having all code in your web application.
Similar as previous one, you should only invoke the restful uri to have your action done. As possible disadvantage, you have to get sure that that uri is not accessible to end users. In usual architecture (Web Server --> Application Server --> DB) this restful service should be in the Application Servers, far away from end user access.
Windows Service
Another option is creating a Windows Service that runs all the time and check the time itself so every hour perform the job (maybe using Quartz or similar). But this does not meet your performance requirements.
The performance hit will be small anyway as your service should check every minute to see if an hour has pass and is time to do its job.. a task pretty easy.
The advantage is that a windows service is easier to control and monitor than a Scheduled tasks
DB job
Yet another option... If your app uses SQL Server you can have a t-sql job that runs daily or hourly. I wouldn't recommend this option unless you really have performance problems.
The problem with this is that you would be splitting the logic and responsibilities of your code. A future developer or admin would find hard to maintain your app.
If you'd like to keep the logic within the web application for simplicity (depending on the total size of your solution, this may or may not be desired):
For a given URL, have the web app check for due approvals and sends emails out if needed. Be sure to keep track of emails sent in order to prevent sending the same email multiple times.
Call this URL in a regular interval. You may use a scheduled task or a third party url monitoring service to do this.
You may call the URL with a simple VBScript (or wget, or curl, or powershell, or whatever is fastest for you), which in turn you can automate by using the task scheduler (see also).
An example script in vbscript for calling an URL:
Function LoadUrl(url)
Dim objRequest
Set objRequest = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objRequest.open "POST", url , false
objRequest.Send
LoadUrl = objRequest.responseText
Set objRequest = Nothing
End Function
Checking every hour won't affect performance. Even checking every minute is probably fine, depending on your database. The simplest option is a console program fired as a Scheduled Task. You can also try a Windows Service but they're a bit trickier.
Also give some thought how you'll count the 48 hours. If an employee puts in expenses just before the weekend then 48 hours will probably elapse every time and you'll end up with a manager having lots of emails in their Inbox on Monday morning. That could cause some friction :)
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.
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.
I want to develop an Online Reminder service in ASP.NET 2.0 (C#) and SQL2005. But I am not getting the concept of reminder service. What I know is using an online reminder service I can schedule a reminder for future dates, which is sent to me (who schedule reminder) via email or SMS on that date. But in asp.net how to do this, caz anyone can schedule a reminder for any date, how we'll know that when to send that mail to the person. We have to put some loop or what.
So please guide me, what is the concept of an online reminder service and how I can easily develop this application using ASP.NET and SQL
Edited
I am on Shared hosting server, so that solution must be able to work on shared hosting.
Or
Please tell me if anyone knows about any FREE and open-source reminder service CMS which I can download and study it.
Microsoft SQL Server 2005 have scheduling (sql jobs) and email features. You may even donot need to use ASP.NET.
Ideally, you would have a windows service that would periodically (every few minutes) check if any new reminders need to be sent out. Since you are on shared hosting, you probably can't install a service though.
I'm not very familiar with windows shared hosting, but if you have the option of creating scheduled/cron job type tasks you could probably do it that way.
If you can't create a scheduled task on your server, another option would be to create a scheduled task on your home PC with a program/script that runs every few minutes and simply hits a special web page on your site. That page could then have the code that checks for reminders and sends them out. It's a bit of a hack, but it should work.
Have a look at Quartz.Net (http://quartznet.sourceforge.net/). You can create an instance of the quartz scheduler in your Application_Start event and as long as the ASP.Net application is running, it will poll the database and trigger any functions you have registered with it. Since you are on a shared host environment, this is probably your best bet unless your hosting provider has a scheduler that can trigger a WebForm (or ASP.Net MVC Controller) periodically.
First you will obviously need to create a user interface and database to store the reminders. That part you got. The next step is to create a service which periodically queries the database for reminders that are due for notification.
The best way to do this is to write a lightweight Windows Service which, as you suggest, uses a loop and a reasonable sleep time (so as not to monopolize the CPU) to continually check the database for reminders and dispatches notifications. It then processes each reminder based on your requirements.
But since you are on shared hosting, you can't deploy a Windows Service, so the next best thing is to run a background thread on Application_Start of your global.asax. There are many examples of how to do this, e.g.:
http://www.west-wind.com/WebLog/posts/67557.aspx
What are some best practices for managing background threads in IIS?
Shared hosting will not work well with what you are trying to do. You could create a background polling thread on Application start, but it will get shut down at some point and may actually be prohibited by your hosting company. An infinite loop will most likely be detected by your hoster and result in your account being automatically shut down, especially if it is using a fair bit of CPU. As John suggests, there may be a scheduled tasks or hosted cron option with your ISP, but generally, those are just for doing things like nightly backups, not really having the level of granularity you need.
Simple answer is, you most likely need something other than a hosted account. You may need to look into a VPS shared hosting service or you may wish to consider looking into MS Azure or Amazon EC2. To do this right, you need to create an application, or better, a service that runs constantly, something a shared hosting account will not provide.
There also a few services out there who can call a specific web page on your service periodically. You could use that to make the page check if there are any reminders that need to be sent.
However since you're then relying on an external site you can't control this might not be the ideal solution if it is very important that the reminders are always being sent.
1) Create a database for storing messages, with a datestamp
2) Create an SQL job, that selects all messages in a time period
3) From the SQL job, you can initialize an .net based SQL Function, that would send out the emails with the System.Net.Mail namespace.
You might consider a 'hack' using the Cache expiration in for triggering events. Create new cache keys that expire at specific Date-Times to run the reminder or make it recur at defined intervals, checking a queue to see if anything new should be sent.
See:
Easy Background Tasks in ASPNET