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:
System.Threading.Timer
in the constructor of
my web service (right next to the commented out line:
//InitializeComponent();
).System.Threading.Timer
I should not have to run
keep-alive code according to msdn Tip #6 below. Questions:
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?It seems there are issues using timers in web services with regards to:
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With