5

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.”

Community
  • 1
  • 1
user610064
  • 481
  • 2
  • 11
  • 25
  • 3
    I'll comment first (based on the title) and _then_ read: please, please, don't do it! – Grant Thomas Oct 25 '12 at 14:23
  • Having now read, may I please refer you to a previous (though not only) answer of mine on this topic - never mind 'but that's different', what I say amounts to the same: http://stackoverflow.com/a/5553048/263681 Lastly, I think you misunderstand what you think you understand amount the Timer keeping your application alive. – Grant Thomas Oct 25 '12 at 14:27
  • Readers - This method was/is used to update badges on SO. The trick is to use expiring cache times. See this old post by Jeff Atwood: https://stackoverflow.blog/2008/07/18/easy-background-tasks-in-aspnet/ or another one by Omar Al Zabir on Code Project: https://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc – Yogi Mar 17 '19 at 22:23

3 Answers3

4

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • OK thanks for this advice which I have read here and there but yet contemplated doing it anyways... I have started a windows service project to do this thing properly. – user610064 Oct 26 '12 at 19:13
2

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

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
0

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

JG in SD
  • 5,427
  • 3
  • 34
  • 46