Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET scheduled jobs

I want to send mails and SMSs in a scheduled time. I would prefer not using a Windows Service.

Is there any options to schedule a task in a custom time in ASP.Net?

Please suggest which is best way.

Thanks in advance.

like image 546
Arun Raj Avatar asked Oct 20 '25 04:10

Arun Raj


1 Answers

I was facing a similar problem once when my website was hosted on a Godaddy shared hosting. I wanted to run scheduled tasks for sending emails without any windows service.

Finally, I resolved the issue by the use of cache.

A cache item expires on a given time or duration. In ASP.NET, you can add entries in the Cache and set an absolute expiry date time, or you can set a duration after which the item is removed from the cache. You can do this by utilizing the following method of the Cache class.

    public void Insert ( System.String key , System.Object value , 
                     System.Web.Caching.CacheDependency dependencies , 
                     System.DateTime absoluteExpiration , 
                     System.TimeSpan slidingExpiration , 
                     System.Web.Caching.CacheItemPriority priority , 
                     System.Web.Caching.CacheItemRemovedCallback onRemoveCallback )

The onRemoveCallback is a delegate to a method which is called whenever a cache item expires. In that method, we can do anything we like. So, this is a good candidate for running code periodically, consistently without requiring any page visit.

This means, we can simulate a Windows Service utilizing Cache timeout.

You can find the entire details on the codeproject url http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc.

like image 78
milan m Avatar answered Oct 21 '25 19:10

milan m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!