Background: I have a .NET 4.0 web service running on Windows Server 2008 written in C #. I would like to start a timer inside the asp.net web service that runs the code once a day. I have heard of some limitations with this (see below), but I am asking if what I plan is to circumvent some limitations and save the code inside the web service, as this is not critical code. However, the accident is unacceptable. Note. I cannot and cannot use WCF.
Plan:
- Declare and create an instance of
System.Threading.Timer in the constructor of my web service (next to the commented line: //InitializeComponent(); ). - Countdown timer every 30 minutes.
- Note. Since I use
System.Threading.Timer , I do not need to run keep-alive in accordance with msdn Tip # 6 below. - If the current time is within 30 minutes of the value of my database (the desired time to run the code), then enter the 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 works correctly, say more than 97% of the time? - Will this plan work when the application pool is redesigned and when the IIS server restarts?
There seems to be problems using timers in web services regarding:
- recycle application pool
- streams die
- no one gets on the web for a long period
- memory problems (it is better to use the Windows service)
Literature:
ASP.NET site - running code at a specific time
Website timer for activating 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 tooltip # 6 , the Timer class found in the System.Threading namespace is a wonderfully useful but lesser-known class in the .NET Framework, at least for web developers. Once created, the timer will call the specified callback in the thread from ThreadPool with a configurable interval. This means that you can configure the code to run without an incoming request in your ASP.NET application, an ideal situation for background processing. You can perform tasks such as indexing or sending emails in this background process then same ".
source share